Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Try
            Dim SqlQuery As String = "UPDATE CustomerT SET [Contact] ='" & txtcontact.Text & _
                                                                "', [FirstName] ='" & txtfirstname.Text & _
                                                                "', [LastName] ='" & txtlastname.Text & _
                                                                "', [Address] ='" & txtaddress.Text & _
                                                                "', WHERE CustomerID = '" & txtid.Text & ";"
            Dim SqlCommand As New OleDbCommand
            With SqlCommand
                .CommandText = SqlQuery
                .Connection = conn
                .ExecuteNonQuery()
            End With
            loadlistView()
            MsgBox("One Record Successfully Updated. . .")

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub
Posted
Updated 9-Mar-14 5:25am
v2

You forgot a quote before the semicolon and you shouldn't put a comma before the WHERE keyword. Also, you use string concatenation to create the queries. That's not a good idea, because now you aren't protected against SQL Injection[^]. Use parameterized queries instead. Using parameterized queries also improves the readability and you'll make less syntax errors:
VB
Dim SqlQuery As String = "UPDATE CustomerT SET [Contact] = @Contact, [FirstName] = @FirstName, [Address] = @Address WHERE CustomerID = @ID;"
Dim SqlCommand As New OleDbCommand
With SqlCommand
    .CommandText = SqlQuery
    With .Parameters
        .AddWithValue("@Contact", txtcontact.Text)
        .AddWithValue("@FirstName", txtfirstname.Text)
        .AddWithValue("@LastName", txtlastname.Text)
        .AddWithValue("@Address", txtaddress.Text)
        .AddWithValue("@ID", txtid.Text)
    End With
    .Connection = conn
    .ExecuteNonQuery()
End With
 
Share this answer
 
v2
Comments
Member 10655667 9-Mar-14 14:23pm    
thanks a lot men. . . sir :D
Member 10655667 9-Mar-14 14:23pm    
thanks a lot men. . . sir :D
Thomas Daniels 9-Mar-14 14:36pm    
You're welcome!
"', WHERE CustomerID = '" & 


just remove the comma before where keyword and it should work.
 
Share this answer
 
Comments
Member 10655667 9-Mar-14 12:18pm    
new error sir ... it says "Syntax Error in query expression 'CustomerID = '0;'." <--on the equals number, anything that i take edit in data. .ex. i edit the number 4 the error says "Syntax Error in query expression 'CustomerID = '4;'." and so on. . .im new in vb.net i dont knowwto do

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