0

I am using a stored procedure to get details.

In that stored procedure 4 parameters are defined

BAL Layer

Sqlparameter[] param=new SqlParameter[4];
param[0]=new Sqlparameter("@one",Uname);
param[3]=new SqlParameter("@three",Pass);

ds=dal.getdetails("spname",param);
return ds;

DAL Layer

public DataSet getdetails(string spname,SqlParameter[] param);
{
      //  here when i retrive the parameter sent by the BAL Layer,
      //I retreive in this  format
      param={"@one",null,null,"@three"}
       //   it throw me error...
       // what code i have to write here...
 }

How can I use only those parameters I needed in select statement...

My insert and select query are in the same stored procedure.

3
  • 1
    if those parameters don't have defaults specified in the stored procedure, you need to pass them in, even if you just set the values to null. Commented Oct 19, 2013 at 5:01
  • that means now i have to set the default values to the stored procedure parameter. Commented Oct 19, 2013 at 5:14
  • My insert and select query are in the same stored procedure - that seems like a really bad idea. Any procedure should have one task - and one task only. If you need an INSERT - create an insert procedure. If you need a SELECT, create a separate select procedure. By doing so, you only ever need to pass in those parameters that are actually needed for each task. Commented Oct 19, 2013 at 7:20

1 Answer 1

3

You need to pass all the parameter.

If the value is null pass db.null value in the parameter DBNull.Value;

Sqlparameter[] param=new SqlParameter[4];
param[0]=new Sqlparameter("@zero",Uname);
param[1]=new Sqlparameter("@one",DBNull.Value);
param[2]=new Sqlparameter("@two",DBNull.Value);
param[3]=new SqlParameter("@three",Pass);

ds=dal.getdetails("spname",param);
return ds;
Sign up to request clarification or add additional context in comments.

5 Comments

i have tried those things...but here parameter name is null,and it will throw me error..
m not trying yet...but i hope its gonna worked for me...and thanks again @Shekhar
One more thing @shekhar, in every where i have to define param[] values 'DBNull.value'...is there any way to programtically set Parameter name if it is null ????
you can set it programmatic to
okkk so pls give me any sample code....and the parameters name defined in code behind must be same as in stored procedure also...???

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.