Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a code below, this code updates the dropdownlist in the gridview. The database is in MS Access
I get the error: "Parameter @RateCenterName has no default value."


RateCenterID is the primary key


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


    string 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("TextBox1");
            TextBox quantityThreshold = (TextBox)row.FindControl("TextBox2");


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


            try
            {
                conn.Open();
                cmd = new OleDbCommand(updateSql, conn);
                cmd.Parameters.Add("@RateCenterName", OleDbType.VarChar).Value = rateCenterName.Text;
                cmd.Parameters.Add("@Province", OleDbType.VarChar).Value = ddl.SelectedValue;
                cmd.Parameters.Add("@QuantityThreshold", OleDbType.VarChar).Value = quantityThreshold.Text;


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

                GridView1.EditIndex = -1;
                GridView1.DataBind();
            }
            catch (OleDbException ex)
            {
                throw (ex);
            }
            finally
            {
                cmd.Dispose();
                conn.Close();
                conn.Dispose();
            }
        }


Kindly help me to solve this issue.


Regards,
Arjun
Posted
Updated 22-Feb-12 0:13am

I think the problem could be that the name of parameter is mismatching in code and stored procedure/query. and the filed that is giving the error is set as NOT NULL. double check spellings to make sure this is not the case.

changing the table definition to accepts nulls could also do the trick but perhaps hat a bad idea design-wise(i don't know the business logic here)
 
Share this answer
 
try following when adding parameters

cmd.Parameters.Add(new OleDbParameter("@RateCenterName", rateCenterName.Text));

also you have to provide parameter @RateCenterID
 
Share this answer
 
v2
Comments
ArjunBabu 22-Feb-12 7:03am    
Kashif Atiq: now I have added the RateCenterID


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


cmd.Parameters.Add("@RateCenterID", OleDbType.Integer).Value = ratecenterid.Text;

Now the error is different,

"No value given for one or more parameter"
@RateCenterID is missing please add and try then
 
Share this answer
 
Comments
ArjunBabu 22-Feb-12 7:03am    
@Sudip Saha: now I have added the RateCenterID


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


cmd.Parameters.Add("@RateCenterID", OleDbType.Integer).Value = ratecenterid.Text;

Now the error is different,

"No value given for one or more parameter"
Guys now I have added the RateCenterID


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


cmd.Parameters.Add("@RateCenterID", OleDbType.Integer).Value = ratecenterid.Text;

Now the error is different,

"No value given for one or more parameter"
 
Share this answer
 
v2
Comments
kashif Atiq 22-Feb-12 6:53am    
in your code check if "RateCenterName" and "QuantityThreshold" empty then supply any default values for them

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