SQLParameter not working properly
I am trying to access a stored procedure a开发者_开发百科nd I'm getting an error that says:
Procedure or function 'getbug' expects parameter '@bugID', which was not supplied.
This is my code to call the procedure.
SqlCommand cmd = new SqlCommand("getbug", cn);
cmd.Parameters.Add(new SqlParameter("bugID", bugID));
bugID is set as 1089 (and is type int)
I can't figure out why this won't work.
Try this instead
SqlCommand cmd = new SqlCommand("getbug", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@bugID", bugID));
Try adding the "@" to the parameter
SqlCommand cmd = new SqlCommand("getbug", cn);
cmd.Parameters.Add(new SqlParameter("@bugID", bugID));
More code will help. Here's a couple of ideas though.
If you're calling a stored procedure, you need to specify the CommandType
. Also, you can use the AddWithValue
method to shorten your code.
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...
}
}
精彩评论