0

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!

1

1 Answer 1

2

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) & "'"
Sign up to request clarification or add additional context in comments.

2 Comments

You da man! Funny how such a small thing can ruin everything! It's working perfectly now. Thanks!
@AaronHurst Does it work if the user's last name is O'Reilly?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.