Click here to Skip to main content
15,902,492 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am develop windows application using c# 2010, i am try to update my data base values using update query, but error is came

Incorrect syntax near the keyword 'where'.


this is for my update code

C#
SqlConnection con1 = new SqlConnection(db.Connectionstring());
                   con1.Open();
                   SqlCommand cmmds = new SqlCommand("update pqsub set quantity=quantity+" + txtqty.Text + "  where kuligai='" + txtkuligai.Text + "' and color ='" + txtcolor.Text + "' and meters='" + txtmeter.Text + "' ", con1);
                   cmmds.ExecuteNonQuery();
                   con1.Close();



any one give me ideas.

What I have tried:

Incorrect syntax near the keyword 'where'.
Posted
Updated 29-Jul-16 22:04pm
Comments
Boopalslm 30-Jul-16 3:55am    
I got answer..... Any one thanks to try my error.

1 solution

The important thing is: don't do it like that.
Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
Fixing that will also fix the problem you have noticed...
C#
using (SqlConnection con1 = new SqlConnection(db.Connectionstring()))
   {
   con1.Open();
   string strSQL = "UPDATE pqsub SET quantity=@QTY WHERE kuligai=@KUL AND color=@COL AND meters=@MET";
   using (SqlCommand cmmds = new SqlCommand(strSQL, con1))
      {
      cmmds.Parameters.AddWithValue("@QTY", txtqty.Text);
      cmmds.Parameters.AddWithValue("@KUL", txtkuligai.Text);
      cmmds.Parameters.AddWithValue("@COL", txtcolor.Text);
      cmmds.Parameters.AddWithValue("@MET", txtmeter.Text);
      cmmds.ExecuteNonQuery();
      }
   }
But if any of those are supposed to be numerical values, you should always check and convert them to an appropriate number variable type using TryParse and pass the number instead. If you start trying to pass user input numbers direct to your DB you are in for a world of pain later on!
 
Share this answer
 
v2

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