Syntax Error in SQL UPDATE Statement

aw88750

Member
Joined
Oct 24, 2014
Messages
5
Programming Experience
Beginner
VIS, I receive this error Incorrect syntax near '('.
and this is my code
cmd.CommandText =
"UPDATE tblproduct SET (id='" & txtid.Text & "',productname='" & txtproductname.Text & "',quantity='" & txtquantity.Text & "',price='" & txtprice.Text & "')"

cmd.ExecuteNonQuery()
please help
 
@pri1493



here is working insert statement
cn.Open()

cmd.CommandText =
"INSERT INTO tblproduct (productname,quantity,price) VALUES ('" & txtproductname.Text & "','" & txtquantity.Text & "','" & txtprice.Text & "')"

cmd.ExecuteNonQuery()

cn.Close()
 
Please don't hijack another, especially an old one, to ask an unrelated question. I have created a new thread for you. Please do so yourself in future.

As for the issue, if you look up the syntax for an UPDATE you will see that there are no parentheses used where you have used them.

You have bigger issues though. You should never use string concatenation like that for SQL code. Follow the Blog link in my signature below and check out my post on Parameters In ADO.NET.
 
testvbdotnet.png
I want to Click on datagrid view row and selected value show in textboxes, and after changing when I click on update button record will update . pls help I am new in vb.net
 
Thanks sir this code is works

Dim strQuery = "UPDATE tblproduct SET productname='" & txtproductname.Text & "' , quantity= '" & txtquantity.Text & "' , price = '" & txtprice.Text & "' WHERE id ='" & txtid.Text & "' "

cmd =
New SqlCommand(strQuery, cn)

cn.Open()

cmd.ExecuteNonQuery()

cn.Close()


Regards,
A.Waheed
 
Thanks sir this code is works

Dim strQuery = "UPDATE tblproduct SET productname='" & txtproductname.Text & "' , quantity= '" & txtquantity.Text & "' , price = '" & txtprice.Text & "' WHERE id ='" & txtid.Text & "' "

cmd =
New SqlCommand(strQuery, cn)

cn.Open()

cmd.ExecuteNonQuery()

cn.Close()


Regards,
A.Waheed

You are still using string concatenation so, while that may work in the cases you're using, it has a number of problems that can lead to syntax errors or worse with certain data. I cannot recommend strongly enough that you do as suggested in my previous post and read my blog post on using parameters.

By the way, when posting code snippets, please post them as plain text inside the appropriate formatting tags, i.e.

[xcode=vb]your code here[/xcode]

Your code would then look like this:
Dim strQuery = "UPDATE tblproduct SET productname='" & txtproductname.Text & "' , quantity= '" & txtquantity.Text & "' , price = '" & txtprice.Text & "' WHERE id ='" & txtid.Text & "' "

cmd =
NewSqlCommand(strQuery, cn)
cn.Open()
cmd.ExecuteNonQuery()
cn.Close()
While it's not an issue in this specific case, one of the most important features of those [xcode] tags is that they maintain indenting, which is critical to making code easy to read.
 
Having said all that, why are you calling ExecuteNonQuery at all? From the looks of your form, what you should be doing is using a data adapter to populate a DataTable, binding that to a BindingSource and binding that to both your grid and your TextBoxes. As the user makes a selection in the grid, the corresponding fields will automatically be displayed in the TextBoxes and as they make changes in the TextBoxes the data in the grid will be automatically updated. When you're ready to save, you use the same data adapter to save all the changes from the DataTable in one go.
 
Back
Top