Error on .ExecuteNonQuery() in SQL Update Query [duplicate]

落花浮王杯 提交于 2019-12-02 12:10:41

your query syntax is wrong. Since you are using params, use placeholders in the SQL: (the question marks are not some 'etc' type thing, you use ? to mark parameters!):

Dim sqlQuery As String = "UPDATE tbl_empinfo SET FirstName = ?, 
             LastName=?, Department=?,
             Status=?, Years=? WHERE empID = ?"

Note: Six parameters

' USING will dispose of the cmd when it is done with it
' ...can also set the SQL and connection props in the constructor:
Using cmd As New OleDbCommand(sqlQuery, con)
   With cmd
       ' no reason to move Textboxes to a variable either:
       .Parameters.AddWithValue("@p1", empFnameText.Text)
       .Parameters.AddWithValue("@p2", empLnameText.Text)
       .Parameters.AddWithValue("@p3", DeptText.Text)
       .Parameters.AddWithValue("@p4", StatText.Text)
       .Parameters.AddWithValue("@p5", yearstext.Text)

your missing 6th parameter:

      .Parameters.AddWithValue("@p6", eNumText.Text)        
      .ExecuteNonQuery()
   End With
 End Using

I dont think Access supports named params, so you use dummy ones but be sure to AddWithValue in the order specified in the SQL string.

EDIT

You can just create a SQL string with the values embedded instead of using params which is sort of what your SQL string does. Params are much better (research SQL injection attacks), but your string method is wrong (and you cant mix methods). It should be:

Dim sqlQuery As String = "UPDATE tbl_empinfo " & 
       "SET FirstName = " & empFname & ", LastName=" & empLname 

The variables have to be outside the quotes or you will be setting FirstName to the literal "empFname"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!