Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

This is my code. I have a 4 columns in a grid view, one of the column has a drop down list as the input control, 2 are text box controls and 1 is a label control (primary key).

I get the error when the update action is clicked.
Error: **No value given for one or more parameters.**

C#
string updateSql = "UPDATE [RateCenters] " + "SET [RateCenterName] = @RateCenterName, [Province]= @Province, [QuantityThreshold] =@QuantityThreshold  " + "WHERE [RateCenterID]=@RateCenterID";


        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];

            DropDownList ddl = (DropDownList)row.FindControl("DropDownList2");

            TextBox rateCenterName = (TextBox)row.FindControl("txtRateCenterName");

            TextBox quantityThreshold = (TextBox)row.FindControl("txtQuantityThreshold");

            Label ratecenterid = (Label)row.FindControl("Label1");

            OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ToString());

            OleDbCommand cmd = null;

            try
            {
                conn.Open();

                cmd = new OleDbCommand(updateSql, conn);

                cmd.Parameters.Add("@RateCenterID", OleDbType.Integer).Value = Convert.ToInt32(ratecenterid.Text);
                cmd.Parameters.Add("@RateCenterName", OleDbType.VarChar).Value = rateCenterName.Text;
                cmd.Parameters.Add("@Province", OleDbType.VarChar).Value = ddl.SelectedValue;
                cmd.Parameters.Add("@QuantityThreshold", OleDbType.Integer).Value = Convert.ToUInt32(quantityThreshold.Text);


                cmd.Connection = conn;
                cmd.ExecuteNonQuery();

                GridView1.EditIndex = -1;
                GridView1.DataBind();
            }

            catch (OleDbException ex)
            {
                throw (ex);
            }

            finally
            {
                cmd.Dispose();
                conn.Close();
                conn.Dispose();
            }
        }
Help me to solve this issue.

regards,
Arjun
Posted
Updated 22-Feb-12 3:02am
v2
Comments
André Kraak 22-Feb-12 9:03am    
Edited question:
Added pre tags

If i remember correctly ..i faced similar issue before and was because of null value getting passed to the parameterized query.
 
Share this answer
 
Comments
ArjunBabu 22-Feb-12 9:27am    
@Rishikesh_Singh: Yeah! How did you clear it? I am too facing the same error
Rishikesh_Singh 22-Feb-12 9:37am    
Use Sqlprofiler and see exactly what is the query generated when you submit.
I think i just checked if the value is null pass empty.
ArjunBabu 22-Feb-12 10:20am    
how to eradicate the error?
If the value you are assining to the parameter is null it will give message like what you have got.Please note database doesn't know about null, to pass null in table you have to pass "DBNull.Value", which represent non existent data.
If you pass null internally the query generated will try to assign Default Constraint Value for that column, so if there is default constraint on your column you will not get this error.

You should check like this while passing value to parameterized query.
C#
param[0].Value=myValue==null?(object)DBNull.Value : myValue
or
param[0].Value=myValue??(object)DBNull.Value
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900