SQLParameter is not working properly

I am trying to access a stored procedure and I get an error message:

The procedure or function 'getbug' expects the parameter '@bugID', which was not provided.

This is my code to call the procedure.

SqlCommand cmd = new SqlCommand("getbug", cn);
cmd.Parameters.Add(new SqlParameter("bugID", bugID));

bugID is set to 1089 (and is an int type)

I can’t understand why this will not work.

+3
source share
3 answers

Try this instead

SqlCommand cmd = new SqlCommand("getbug", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@bugID", bugID));
+4
source

Try adding the "@" parameter to the parameter

SqlCommand cmd = new SqlCommand("getbug", cn);
cmd.Parameters.Add(new SqlParameter("@bugID", bugID));
+2
source

. , .

, CommandType. , AddWithValue, .

using (var cn = new SqlConnection("your connection string"))
{
    using (var cmd = new SqlCommand("getbug", cn))
    {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@bugID", bugID);

        //etc...
    }
}
+1

Source: https://habr.com/ru/post/1739793/


All Articles