2014-10-12 67 views
0

我无法弄清楚这是什么问题,当我从列表视图更新记录时出现一个弹出窗口,显示没有给出一个或多个所需参数的值。在更新删除记录时没有给出一个或多个必需参数的值

继承人我的代码:

Private Sub BtnUpdate_Click(sender As Object, e As EventArgs) Handles BtnUpdate.Click 
    Try 
     Dim SqlQuery As String = "UPDATE UsersTable Set AccountType = '" & CmbAccountType.Text & "' , Username = '" & TxtUsername.Text & "' , UserPassword = '" & TxtPassword.Text & "' , Firstname = '" & TxtFirstname.Text & "' , Lastname = '" & TxtLastname.Text & "' , Sex = '" & CmbSex.Text & "',Birthdate = '" & DateTimePickerBirthdate.Text & "' , ContactNumber = '" & TxtContact.Text & "' , Address = '" & TxtAddress.Text & "' WHERE UserID = " & id & ";" 
     Dim SqlCommand As New OleDbCommand 
     With SqlCommand 
      .CommandText = SqlQuery 
      .Connection = Conn 
      .ExecuteNonQuery() 
     End With 
     MsgBox("Account successfully updated!") 
     loadlistview() 
    Catch ex As Exception 
     MsgBox(ex.Message) 
    End Try 
End Sub 

Private Sub BtnDelete_Click(sender As Object, e As EventArgs) Handles BtnDelete.Click 
    Try 
     Dim SqlQuery As String = "DELETE FROM ProductTable WHERE UserID = " & id & ";" 
     Dim SqlCommand As New OleDbCommand 
     With SqlCommand 
      .CommandText = SqlQuery 
      .Connection = Conn 
      .ExecuteNonQuery() 
     End With 
     MsgBox("Account deleted.") 
     loadlistview() 
    Catch ex As Exception 
     MsgBox(ex.Message) 
    End Try 
End Sub 
+1

http://stackoverflow.com/a/26258610/1070452解决您的SQL来使用参数,您可能很容易看到错误。 – Plutonix 2014-10-12 01:16:16

+0

你能举出更新声明的例子吗?我只是一个新手无法遵循它。 – jeromeee 2014-10-12 03:21:26

+0

它是SQL参数的完整且完整的分项描述。插入工作相同 – Plutonix 2014-10-12 11:17:16

回答

0

的问题是在你在查询中使用的WHERE条件。这样你就可以使用下面的查询

纠正在update查询:

Dim SqlQuery As String = "UPDATE UsersTable Set AccountType = '" & CmbAccountType.Text & "'....................." & _ 
"WHERE UserID = '" & id & "';" '<----- you miss a single quote in where clause. 

Delete查询:

Dim SqlQuery As String = "DELETE FROM ProductTable WHERE UserID = '" & id & "';" 
+0

感谢您的回应我做了你现在说的即时通讯在我的程序中得到不同的错误它说:字符串查询表达式中的语法错误'UserId ='1;'。 – jeromeee 2014-10-12 06:02:09

+0

检查你是否给这个'''';''或像这样'';'“' – 2014-10-12 06:28:27

+0

它是这样的> WHERE User ID ='”&id & " ';“ – jeromeee 2014-10-12 08:50:24

相关问题