Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is my code:

C#
if (chkUpdate.Checked)
               {
                   string strID = GridView1.Rows[i].Cells[1].Text;
                 string v_item_no = GridView1.Rows[i].Cells[4].Text;
                   string strName = ((TextBox)GridView1.Rows[i].FindControl("txtName")).Text;
                   string strLocation = ((TextBox)GridView1.Rows[i].FindControl("txtLocation")).Text;

C#
try
                 {
                     const string strUpdate = "Update gvtest set status ='Y' WHERE mem_id = @mem_id and item_no=@v_item_no";
                     cmd.CommandType = CommandType.Text;
                     cmd.CommandText = strUpdate.ToString();
                     cmd.Parameters.Clear();

C#
cmd.Parameters.AddWithValue("@mem_id", strID);
                  cmd.Parameters.AddWithValue("@item_no", v_item_no);
                  cmd.Connection = con;
                  con.Open();
                  cmd.ExecuteNonQuery();
              }
              catch (SqlException ex)
              {
                  string errorMsg = "Error in Updation";
                  errorMsg += ex.Message;
                  throw new Exception(errorMsg);
              }
              finally
              {
                  con.Close();
              }




Message:

Error in updation. Must declare the scalar variable "@v_item_no"

Can anyone identify the error and help?
Posted

Yes:
C#
const string strUpdate = "Update gvtest set status ='Y' WHERE mem_id = @mem_id and item_no=@v_item_no";
...
cmd.Parameters.AddWithValue("@item_no", v_item_no);
The parameter names must match: change last line of the two to:
C#
cmd.Parameters.AddWithValue("@v_item_no", v_item_no);
 
Share this answer
 
Comments
Joezer BH 10-Apr-13 5:03am    
5+
Your query is having the parameter with different name.
SQL
Update gvtest set status ='Y' WHERE mem_id = @mem_id and item_no=@v_item_no

Parameter name should be same at both the places.
Try this:
C#
cmd.Parameters.AddWithValue("@mem_id", strID);
cmd.Parameters.AddWithValue("@v_item_no", v_item_no);





--Amit
 
Share this answer
 
v3
Comments
Joezer BH 10-Apr-13 5:03am    
5+
_Amy 10-Apr-13 5:04am    
Thank you Edo. :)
cmd.Parameters.AddWithValue("@item_no", v_item_no);

Change @item_no to @v_item_no

v_ is missing from the parameter
 
Share this answer
 
Comments
PrashantSonewane 10-Apr-13 4:51am    
Mark as answered if this work for you!

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