Developer
Developer

Reputation: 888

Why am I getting "no overload for method takes two arguments"?

I have a method in one DLL:

public static DataSet ExecuteDataset(string strcommandText,CommandType commandType,SqlParameter[] p)
 {
   CreateConnection();
   da = new SqlDataAdapter(strcommandText, con);
   da.SelectCommand.CommandType = commandType;
   da.SelectCommand.Parameters.AddRange(p);
   ds = new DataSet();
   da.Fill(ds);
   return ds;
 }    

And I have written a method in another DLL:

   public static DataSet GetDeptDetails()
    {
    string strcommandText = "sp_DeptDetails";
    return SqlHelper.ExecuteDataset(strcommandText,CommandType.StoredProcedure);
     }

Here, I'm getting this error:

no overload for method takes two arguments.

What am I doing wrong?

Upvotes: 0

Views: 38524

Answers (3)

Developer
Developer

Reputation: 888

Thanks for Reply Guys.. In above program we can give in sqlhelper class in ExecuteDataset Method parameters as optional parameters or we can give as default parameters. so that sqlparamter passing is not mandatory to other method, we can pass only if require.

Upvotes: 0

Mohit
Mohit

Reputation: 39

You are expecting 3 parameter in

`public static DataSet ExecuteDataset(string strcommandText,CommandType commandType,SqlParameter[] p)`
 method.

But you send only two parameter when you call this method

return SqlHelper.ExecuteDataset(strcommandText,CommandType.StoredProcedure);

So when you call same method by passing two parameters only Sqlhelper object looking for an another method with same name having two parameter which cause this error.

you can resolve problem by passing third parameter or simply set third parameter as null by default.

Upvotes: 0

Mr.LamYahoo
Mr.LamYahoo

Reputation: 1546

public static DataSet ExecuteDataset(string strcommandText,CommandType commandType,SqlParameter[] p=null)
 {
   CreateConnection();
   da = new SqlDataAdapter(strcommandText, con);
   da.SelectCommand.CommandType = commandType;
   if(p!=null)
   {
     da.SelectCommand.Parameters.AddRange(p);
   }
   ds = new DataSet();
   da.Fill(ds);
   return ds;
 } 




public static DataSet GetDeptDetails()
    {
    string strcommandText = "sp_DeptDetails";
    return SqlHelper.ExecuteDataset(strcommandText,CommandType.StoredProcedure);
     } 

Upvotes: 1

Related Questions