James P. Wright
James P. Wright

Reputation: 9131

SQLParameter not working properly

I am trying to access a stored procedure and 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.

Upvotes: 4

Views: 1413

Answers (3)

Aaron Daniels
Aaron Daniels

Reputation: 9664

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

Upvotes: 1

CResults
CResults

Reputation: 5105

Try this instead

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

Upvotes: 4

sergiom
sergiom

Reputation: 4887

Try adding the "@" to the parameter

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

Upvotes: 3

Related Questions