expects parameter '@ID', which was not supplied?
Asked Answered
M

6

21

I am sending ID as outparameter but its giving error

System.Data.SqlClient.SqlException: Procedure or function 'usp_ClientHistoryItem' expects parameter '@ID', which was not supplied.

Code

 using (SqlCommand cmd = new SqlCommand("dbo.usp_ClientHistoryItem", conn))
 {
      SqlParameter parameterID = new SqlParameter("@ID", oReservation.Id);
      parameterID.Direction = ParameterDirection.Output;
      cmd.Parameters.Add(parameterID); 

      cmd.Parameters.Add(new SqlParameter("@PhoneNo", oReservation.ClientPhone));
      cmd.Parameters.Add(new SqlParameter("@UserId", oReservation.UserID));
      cmd.Parameters.Add(new SqlParameter("@Description", oReservation.Description));
      cmd.Parameters.Add(new SqlParameter("@TestId", oReservation.TestId));
      cmd.Parameters.Add(new SqlParameter("@StartDate", oReservation.StartDate));

      cmd.ExecuteNonQuery();

      returnValue = Convert.ToInt32(cmd.Parameters["@ID"].Value);

      return returnValue;
}
Morello answered 21/3, 2012 at 6:18 Comment(1)
did you set the ID parameter as out when creating the stored procedure? (ON THE DATABASE; NOT THE CODE)Lallation
E
47

You seem to be calling a stored procedure - yet you've never defined your SqlCommand to be a stored procedure:

using (SqlCommand cmd = new SqlCommand("dbo.usp_ClientHistoryItem", conn))
{
    cmd.CommandType = CommandType.StoredProcedure;  // add this line to tell ADO.NET it's a stored procedure!!

If you forget that line, then ADO.NET will try to interpret your stuff as an ad-hoc SQL statement....

Excise answered 21/3, 2012 at 6:22 Comment(0)
W
3

this one solve my problem may be it may helpful

cmd.CommandType = CommandType.StoredProcedure;

Witha answered 1/2, 2014 at 9:40 Comment(0)
N
2

Your ID parameter in the stored procedure must be set as OUTPUT parameter. You are just setting it in code not in stored procedure.

Novelette answered 21/3, 2012 at 6:50 Comment(1)
I did it in StoredProcedure :)Morello
L
2

Hy guys.

You have to set the property CommandType for the Command to StoredProcedure if that's the case. Otherwise it woun't detect the parameters.

Layout answered 11/12, 2013 at 12:40 Comment(0)
H
0

One other reason this error is thrown is when the variable names don't match in your stored procedure and code because the code fails to find the parameter to which the value must be passed. Make sure they match:

Stored procedure:

create procedure getEmployee
    @ID 
as
Begin
    select * 
    from emp 
    where id = @ID
End

Code:

SqlParameter p = new SqlParameter("@ID", id);
cmd.Parameter.Add(p);

The parameter @ID must match in both code and stored procedure

Hesta answered 16/7, 2016 at 13:52 Comment(0)
N
0

If you use dapper, you can use this construction

int id = 1;

var parameters = new DynamicParameters();

parameters.Add("@id", id, DbType.Int32, ParameterDirection.Input);

string sqlQuery = "[dbo].[SomeStoredProcedure]";

using (IDbConnection db = new SqlConnection(ConnectionString))
{
    var result = await db.QueryAsync<SpResult>(sqlQuery, parameters, commandType: CommandType.StoredProcedure);  
}

Nashville answered 4/5, 2020 at 10:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.