Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!
This is the code I have used:

sqlinsert.CommandText = "INSERT INTO checkreordercritical VALUES ('" & ds2.Tables("Products").Rows(0).Item(0) & "','" & ds2.Tables("Products").Rows(0).Item(1) & "','" & ds2.Tables("Products").Rows(0).Item(2) & "','" & ds2.Tables("Products").Rows(0).Item(3) & "','" & ds2.Tables("Products").Rows(0).Item(4) & "','" & ds2.Tables("Products").Rows(0).Item(5) & "','" & ds2.Tables("Products").Rows(0).Item(6) & "','" & ds2.Tables("Products").Rows(0).Item(7) & "','" & ds2.Tables("Products").Rows(0).Item(8) & "','" & ds2.Tables("Products").Rows(0).Item(9) & "','" & ds2.Tables("Products").Rows(0).Item(10) & "','" & ds2.Tables("Products").Rows(0).Item(11) & "','" & ds2.Tables("Products").Rows(0).Item(12) & "','" & ds2.Tables("Products").Rows(0).Item(13) & "','" & ds2.Tables("Products").Rows(0).Item(14) & "','" & ds2.Tables("Products").Rows(0).Item(15) & "','" & ds2.Tables("Products").Rows(0).Item(16) & "','" & ds2.Tables("Products").Rows(0).Item(17) & "','" & ds2.Tables("Products").Rows(0).Item(18) & "','" & ds2.Tables("Products").Rows(0).Item(19) & "','Reorder')"



However, I got this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '5 OUNCES','Green','0','0','0','koko','0','60','20','10','0','LA','Re' at line 1

Please help me with this. Thanks in advance!God bless
Posted
Comments
Mahesh Bailwal 26-Aug-13 13:23pm    
Put a break point at sqlinsert.ExecuteNonQuery() and at debug time check the value of sqlinsert.CommandText or paste in notepad and check for the syntax error.

1 solution

Please don;t do it like that! Never concatenate strings to form an SQL query - it leave you wide open to SQL Injection attacks, and makes queryies much harder to read and debug than they need to be. Use Parameterized queries instead:
C#
sqlinsert.CommandText = "INSERT INTO  checkreordercritical VALUES (@C0, @C1, @C2, @C3 ...
sqlinsert.Parameters.AddWithValue("@C0", ds2.Tables("Products").Rows(0).Item(0))
sqlinsert.Parameters.AddWithValue("@C1", ds2.Tables("Products").Rows(0).Item(1))
sqlinsert.Parameters.AddWithValue("@C2", ds2.Tables("Products").Rows(0).Item(2))
...
It makes it easier to read (because you don't need to use the single quotes) and it's safer. Chances are, you syntax error will disappear at the same time, because it is probably caused by a single quote in the data you are assembling your SQL command from!
 
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