Click here to Skip to main content
15,867,939 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
I am creating windows application using c# 2010, here i am using data grid view for billing purpose, but save the grid view values to data base below error is came 

Must declare the scalar variable "@customer".
 
 SqlCommand cmd2 = new SqlCommand("update stkdetails set customer=customer+@customer" + rows.Cells[7].Value + " where empname='" + rows.Cells[2].Value + "'and date='" + txtdate.Text + "'", con2);


any one give me some ideas how to solve above error


What I have tried:

Must declare the scalar variable "@customer".
Posted
Updated 19-Nov-16 3:10am

1 solution

You create parameters and add them to the SQLCommand object:
C#
SqlCommand cmd2 = new SqlCommand("update stkdetails set customer=customer+@customer where empname='" + rows.Cells[2].Value + "'and date='" + txtdate.Text + "'", con2);
cmd2.Parameters.AddWithValue("@customer", rows.Cells[7].Value);

I would also strongly suggest you set up parameters for @empname and @date at the same time.
C#
SqlCommand cmd2 = new SqlCommand("update stkdetails set customer=customer+@customer where empname=@empname and date=@date", con2);
cmd2.Parameters.AddWithValue("@customer", rows.Cells[7].Value);
cmd2.Parameters.AddWithValue("@empname", rows.Cells[2].Value);
cmd2.Parameters.AddWithValue("@date", txtDate.Text);
 
Share this answer
 
v2
Comments
Boopalslm 19-Nov-16 9:15am    
I am trying your code but below error is came

The parameterized query '(@customer nvarchar(4000),@empname nvarchar(4000),@date nvarchar' expects the parameter '@customer', which was not supplied.

how to solve above error. give me some ideas
Midi_Mick 19-Nov-16 9:19am    
What type of field is customer? Is it text or numeric? And what are you adding to it?
Boopalslm 19-Nov-16 9:22am    
customer is data grid view column name, data type float, and than below this is my full code

foreach (DataGridViewRow rows in dataGridView1.Rows)
{



SqlConnection con1 = new SqlConnection(db.Connectionstring());
con1.Open();
SqlCommand cmd1 = new SqlCommand("select * from stkdetails where empname='" + rows.Cells[2].Value + "'", con1);

SqlDataReader dr = cmd1.ExecuteReader();


if (dr.HasRows)
{


SqlConnection con2 = new SqlConnection(db.Connectionstring());
con2.Open();


SqlCommand cmd2 = new SqlCommand("update stkdetails set customer=customer+@customer where empname=@empname and date=@date", con2);
cmd2.Parameters.AddWithValue("@customer", rows.Cells[7].Value);
cmd2.Parameters.AddWithValue("@empname", rows.Cells[2].Value);
cmd2.Parameters.AddWithValue("@date", txtdate.Text);

cmd2.ExecuteNonQuery();
con2.Close();


}

else
{
SqlConnection con3 = new SqlConnection(db.Connectionstring());
con3.Open();
SqlCommand cmd3 = new SqlCommand("insert into stkdetails(date,empname,customer)values('" + txtdate.Text + "','" + rows.Cells[2].Value + "','" + rows.Cells[7].Value + "')", con3);
cmd3.ExecuteNonQuery();
con3.Close();

}


}
Midi_Mick 19-Nov-16 9:30am    
Ok - you may need to make sure that your parameter is the right type:
Try changing the line where you add the @customer parameter to
cmd2.Parameters.AddWithValue("@customer", Convert.ToDouble(rows.Cells[7].Value));

Also worth setting up the query in cmd3 as parameterized too, in just the same way as we just did for cmd2.
Boopalslm 19-Nov-16 9:39am    
I am alter my code as per your way but below error is came

The parameterized query '(@customer float,@empname nvarchar(4000),@date nvarchar(10))inse' expects the parameter '@empname', which was not supplied.

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