protected void gridOmniZone_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = gridOmniZone.Rows[e.RowIndex];
Int64 ID = Convert.ToInt64(gridOmniZone.DataKeys[e.RowIndex].Values[0]);
string Description = (row.Cells[2].Controls[1] as TextBox).Text;
string LatCenter = (row.Cells[3].Controls[1] as TextBox).Text;
string LongCenter = (row.Cells[4].Controls[1] as TextBox).Text;
string Radius = (row.Cells[5].Controls[1] as TextBox).Text;
string Address = (row.Cells[6].Controls[1] as TextBox).Text;
string City = (row.Cells[7].Controls[1] as TextBox).Text;
string State = (row.Cells[8].Controls[1] as TextBox).Text;
string PostalCode = (row.Cells[9].Controls[1] as TextBox).Text;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("dbo.usp_UpdateOmniZone"))
{
cmd.Connection = con;
cmd.Parameters.Add("@ID", SqlDbType.BigInt);
cmd.Parameters[0].Value = ID;
cmd.Parameters.AddWithValue("@Description", Description);
cmd.Parameters.AddWithValue("@LatCenter", LatCenter);
cmd.Parameters.AddWithValue("@LongCenter", LongCenter);
cmd.Parameters.AddWithValue("@Radius", Radius);
cmd.Parameters.AddWithValue("@Address", Address);
cmd.Parameters.AddWithValue("@City", City);
cmd.Parameters.AddWithValue("@State", State);
cmd.Parameters.AddWithValue("@PostalCode", PostalCode);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
gridOmniZone.EditIndex = -1;
this.BindGrid();
}
My code above throws an error on cmd.ExecuteNonQuery
. The error is:
stored procedure expects parameter @ID which was not supplied.
As you can see, I did provide the parameter. Any idea what I am doing wrong? The debugger is telling me that the variable ID
is a valid integer value.
2
Answers
I think the problem is that you forgot to set the CommandType to CommandType.StoredProcedure.
I up-voted the other answer – the issue is that message is common if you don’t set the command type to stored procedure.
And you ARE better off to strong type the values.
So, this:
And you don’t need the "close" connection – quite much the WHOLE point of the "using" block.