Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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

C#
Incorrect syntax near the keyword 'where'.

 SqlCommand cmd2 = new SqlCommand("update stkdetails set 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:

JavaScript
Incorrect syntax near the keyword 'where'.
Posted
Updated 18-Nov-16 14:28pm
Comments
[no name] 18-Nov-16 11:39am    
Yes, use proper parameterized queries and your problem will likely go away all by itself.
Boopalslm 18-Nov-16 11:44am    
rows.Cells[2].Value - this is proper parameterized cell value. but error is came
[no name] 18-Nov-16 11:46am    
No it is not. You have been told this over and over.
Vamsi Krishnna 18-Nov-16 11:45am    
enclose this section in single codes " + rows.Cells[7].Value + ". Problem should be solved

Please read bobby-tables.com: A guide to preventing SQL injection[^], before someone destroys your database.
 
Share this answer
 
This is a very, very simple error to fix on your own because the error tells you exactly what the problem is. I do not mean to sound rude, but it would be much faster for you to fix it than it takes to post this question and wait for a response.

Secondly, use a parameterized query. The way you have your code now, I could hack your db very easily. You have very unsafe code.

Something like:
C#
SqlCommand cmd2 = new SqlCommand("update stkdetails set customer= customer+ @customer where empname=@empName and..., con2);
cmd2.Parameters.AddWithValue("@customer", row.Cells[7].Value);
cmd2.Parameters.AddWithValue("@empName", rows.Cells[2].Value);
...
// you finish the rest.  Very, very simple.
 
Share this answer
 
v2
Comments
Boopalslm 18-Nov-16 11:55am    
below error is came

The parameterized query '(@customer nvarchar(4000),@empName nvarchar(4000))update stkdeta' expects the parameter '@customer', which was not supplied.
ZurdoDev 18-Nov-16 11:59am    
The error, again, is pretty clear. It says you did not provide a value for @customer when you called the sql.
Boopalslm 18-Nov-16 12:08pm    
how to solve the error
Boopalslm 18-Nov-16 12:16pm    
The parameterized query '(@empname varchar(8000),@customer float)update stkdetails set cu' expects the parameter '@empname', which was not supplied.

now above error is came, what can i do give me ideas.
ZurdoDev 18-Nov-16 13:24pm    
You need to do what the error is telling you.
The problem with the way you build the query is that the error or not depend on variables contain.
The variables are promoted to SQL code and a malicious value opens the door to SQL Injection. The use of parameters may br the solution to both problems.
SQL injection - Wikipedia[^]
SQL Injection[^]
 
Share this answer
 

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