Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

expects parameter '@ID', which was not supplied?

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;
}
like image 615
Azhar Avatar asked Mar 21 '12 06:03

Azhar


3 Answers

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....

like image 198
marc_s Avatar answered Oct 18 '22 06:10

marc_s


this one solve my problem may be it may helpful

cmd.CommandType = CommandType.StoredProcedure;

like image 32
siva Avatar answered Oct 18 '22 06:10

siva


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

like image 2
Mujtaba Hassan Avatar answered Oct 18 '22 06:10

Mujtaba Hassan