Aaron Hurst
Aaron Hurst

Reputation: 121

Error with VB.NET/SQL Server Update Statement

Background: I have a Windows form in Visual Studio 2010 with VB.NET code. On the form I have a combo box where I can select a record from my SQL table. When a selection is made, several text boxes are then filled with my SQL table data.

Problem: I need to edit the information that is dumped into those text boxes. By edit, I mean I need to change the contents of the text boxes on the Windows form, Click my Update button, and the contents of my SQL table be Updated.

Here is my code:

    Dim con As New SqlConnection
    Dim conDim cmd As New SqlCommand
    Try
        con.ConnectionString = "Server=fakeservername; Database=fakedatabasename; integrated security=true"
        con.Open()
        cmd.Connection = con
        cmd.CommandText = ("UPDATE Users " & _
                           "SET User_FName = '" & Trim(txtFName.Text) & "'," & _
                           "User_LName = '" & Trim(txtLName.Text) & "' ," & _
                           "User_Address = '" & Trim(txtAddress.Text) & "'," & _
                           "User_City = '" & Trim(txtCity.Text) & "'," & _
                           "User_State = '" & Trim(txtState.Text) & "'," & _
                           "User_Zip = '" & Trim(txtZip.Text) & "'," & _
                           "User_Phone = '" & Trim(txtPhone.Text) & "'," & _
                           "User_AltPhone = '" & Trim(txtAltPhone.Text) & "'," & _
                           "WHERE Users.User_ID ='" & (txtUserID.Text) & "';")
        cmd.ExecuteNonQuery()
    Catch ex As Exception
        MessageBox.Show("Error while inserting record on table..." & ex.Message, "Insert Records")
    Finally
        con.Close()
    End Try

Here is my error:

"Incorrect syntax near the word 'Where'."

I've looked all over the internet for the answer to this question. I think I'm tip-toeing around the answer but I just can't put my finger on it. So, I'm coming to you all. Any suggestions? Thanks in advance!

Upvotes: 0

Views: 1414

Answers (1)

Sunny
Sunny

Reputation: 4809

There is additional comma in the last column before where condition that is not required.

"User_AltPhone = '" & Trim(txtAltPhone.Text) & "',"

should be

"User_AltPhone = '" & Trim(txtAltPhone.Text) & "'"

Upvotes: 2

Related Questions