Risky Wahyu N Risky Wahyu N - 4 months ago 7
SQL Question

Incorrect UPDATE syntax near '='

I got some error "incorrect syntax near '='" in update.

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
conn.Open()
For i = 0 To Me.Dgv_sql.Rows.Count - 1
Dim idx As String = Dgv_sql.Rows(i).Cells("ID").Value
query = "UPDATE kkpsurabaya SET late = '" & Dgv_sql.Rows(i).Cells("late").Value & "' WHERE ID = " & idx

If (UpdateData(query)) Then

End If

Next
conn.Close()
MessageBox.Show("Data Berhasil Di Update", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information)
'dgv_sql_isi()
End Sub


enter image description here

Answer

Idx is a string so when you just do where ID = (and place string here) it'll fail.

Change to:

query = "UPDATE kkpsurabaya SET late = '" & Dgv_sql.Rows(i).Cells("late").Value & "' WHERE ID = '" & idx &"'"

But a better solution will just be to use Parameterized Queries all together.. - Will help you avoid SQL Injection

Comments