0

I have written the code below to update my table, but it doesn't work.

sqlstr = "UPDATE Student SET " & S & " = @field1, " & L & " = @field2, " & R & " =@field3, " & W & "=@field4 WHERE Code='" & StdID & "'"
DBCmd = New MySql.Data.MySqlClient.MySqlCommand(sqlstr, DBConn)
With DBCmd
        .Parameters.AddWithValue("@field1", CB_S.SelectedItem)
        .Parameters.AddWithValue("@field2", CB_L.SelectedItem)
        .Parameters.AddWithValue("@field3", CB_R.SelectedItem)
        .Parameters.AddWithValue("@field4", CB_W.SelectedItem)
    End With
    DBCmd.Dispose()

Where S, L, R and W are strings: S1, L1, R1, W1. And StdID is an integer. CB_S, CB_L, CB_R and CB_W are comboboxes.

Could anyone tell me what is wrong?

0

1 Answer 1

2

you did not open the connection and call ExecuteNonQUery(). Also parameterized the value for Code

sqlstr = "UPDATE Student SET " & S & " = @field1, " & L & " = @field2, " & R & " =@field3, " & W & "=@field4 WHERE Code=@code"
DBCmd = New MySql.Data.MySqlClient.MySqlCommand(sqlstr, DBConn)
With DBCmd
        .Parameters.AddWithValue("@field1", CB_S.SelectedItem)
        .Parameters.AddWithValue("@field2", CB_L.SelectedItem)
        .Parameters.AddWithValue("@field3", CB_R.SelectedItem)
        .Parameters.AddWithValue("@field4", CB_W.SelectedItem)
        .Parameters.AddWithValue("@code", StdID)
End With
DBConn.Open()
DBCmd.ExecuteNonQuery()
Sign up to request clarification or add additional context in comments.

1 Comment

Oh, That's embarrassing!! Thanks!

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.