Home > Net >  SQLCommand AddWithValue - What am I doing wrong?
SQLCommand AddWithValue - What am I doing wrong?

Time:08-24

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

CodePudding user response:

I think the problem is that you forgot to set the CommandType to CommandType.StoredProcedure.

CodePudding user response:

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:

using (SqlConnection con = new SqlConnection(constr))
{
    using (SqlCommand cmd = new SqlCommand("dbo.usp_UpdateOmniZone",con))
    {
        cmd.Parameters.Add("@ID", SqlDbType.BigInt).Value = ID;
        cmd.Parameters.Add("@Description",SqlDbType.NVarChar).Value = Description;
        cmd.Parameters.Add("@LatCenter", SqlDbType.NVarChar).Value = LatCenter;
        cmd.Parameters.Add("@LongCenter", SqlDbType.NVarChar).Value = LongCenter;
        cmd.Parameters.Add("@Radius", SqlDbType.NVarChar).Value = Radius;
        cmd.Parameters.Add("@Address", SqlDbType.NVarChar).Value = Address;
        cmd.Parameters.Add("@City", SqlDbType.NVarChar).Value = City;
        cmd.Parameters.Add("@State", SqlDbType.NVarChar).Value = State;
        cmd.Parameters.Add("@PostalCode", SqlDbType.NVarChar).Value = PostalCode;

        cmd.CommandType = CommandType.StoredProcedure;
        con.Open();
        cmd.ExecuteNonQuery();
    }
}

And you don't need the "close" connection - quite much the WHOLE point of the "using" block.

  • Related