Procedure or function 'x' expects parameter '@y', which was not supplied.Got error on c#?

4.7k views Asked by At

First at all, i know questions like these are duplicate BUT, please pay attention to this expect one. Here is my C# code behind which raise the error as :

Procedure or function 'aaa' expects parameter '@bbb', which was not supplied.

if (Dt.Rows.Count > 0)
{
    Cmd = new SqlCommand();
    Cmd.CommandText = ("[dbo].[aaa]");
    Cmd.CommandType = CommandType.StoredProcedure;
    Cmd.Parameters.Add("@bbb", SqlDbType.Int).Value = 1;
    Cmd.Parameters.Add("@LettType", SqlDbType.VarChar, 3).Value = LetterType;
    Cmd.Parameters.Add("@IsTajamo", SqlDbType.Char, 1).Value = 1;
    Da = new SqlDataAdapter(Cmd.CommandText, Con);
    Dt.Clear();
    /*Line72:*/    
    Da.Fill(Dt);
}

Technically, I can't found where is the error. I passed all parameters. And when i execute my stored procedure separately in MS-SQL It work right with these values.

Can you experts please help me on this issue? Attention : here is Stored Proc :

   ALTER PROCEDURE [dbo].[aaa] (@bbb Int , @LettType CHAR(3) , @IsTajamo CHAR(1))

AS
BEGIN

select 1


END

Attention : (may be this can help, here is my stack-trace) :

at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() at System.Data.SqlClient.SqlDataReader.get_MetaData() at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior) at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable) at WebService.Services.GetLettersList(String UserName, String PassPhrase, String CodeName, String LetterType) in D:\Amiri\WebService\WebService\Services.asmx.cs:line 79

2

There are 2 answers

2
Hans Kesting On BEST ANSWER

In your line:

Da = new SqlDataAdapter(Cmd.CommandText, Con);

you are only passing the name of the procedure and ignoring the parameters. Pass only Cmd (and add that connection to the command) to use your defined params.

0
Crowcoder On

@Hans Kesting is correct, but I don't think it is explained well. What is happening with this...

da = new SqlDataAdapter(Cmd.CommandText, Con);

...is you are implicitly creating a new SqlCommand that has the same CommandText as Cmd. It is the same as if you just literally used [dbo].[aaa] as the argument value. Therefore, this new SqlCommand has no SqlParameters assigned. You could then do: SqlDataAdapter.SelectCommand.Parameters.Add(...., but that is a mess.

What you need to do is use the SqlCommand (Cmd) itself. But you must also make sure it is tied to the connection:

   Cmd = Con.CreateCommand(); //This hooks up the connection to this command
   Cmd.CommandText = "[dbo].[aaa]";
   Cmd.CommandType = CommandType.StoredProcedure;
   Cmd.Parameters.Add("@bbb", SqlDbType.Int).Value = 1;
   Cmd.Parameters.Add("@LettType", SqlDbType.VarChar, 3).Value = LetterType;
   Cmd.Parameters.Add("@IsTajamo", SqlDbType.Char, 1).Value = 1;
   Da = new SqlDataAdapter(Cmd); //Just the command
   Dt.Clear();
   Da.Fill(Dt);