Procedure or function expects parameter which was not supplied

I had some fairly simple database code to call a stored procedure:

using (SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings[“Scott”].ConnectionString))             {                 using (SqlCommand Comm = new SqlCommand(“ImportStuff”, Conn))
{
SqlParameter param = new SqlParameter(“@Name”, SqlDbType.VarChar, 10);
param.Value = “Scott”
Comm.Parameters.Add(param);

param = new SqlParameter(“@Quest”, SqlDbType.VarChar, 50;
param.Value = “Write a blog post”;
Comm.Parameters.Add(param);

param = new SqlParameter(“@FavoriteColor”, SqlDbType.VarChar, 10);
param.Value = “Green”;
Comm.Parameters.Add(param);

Conn.Open();
Comm.ExecuteNonQuery();
}
}

No problem. However, when I ran it, I got there exception:

Procedure or function ‘ImportStuff’ expects parameter ‘@Name’, which was not supplied.

Hmm, everything seemed OK in the stored procedure – @Name was there. And as you can see, it was defined in my C# code. So what is the problem?

I would like to preface my answer by saying I’ve done .NET development for twelve years. Tens of thousands of lines of code, good and bad, have come from my fingers. And in this instance, I missed a very simple line:

Comm.CommandType = CommandType.StoredProcedure;

Yes, I had not told the SqlCommand object that I was calling a procedure than text. And after I fixed it, I hoped that was the dumbest mistake I would make that day.

Advertisements
  1. #1 by relliswp on September 30, 2014 - 11:38 am

    This is a year old, but damned if you didn’t just save me from another hour of frustration. Thanks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: