大家好日子。我想請求幫助我的代碼更新數據庫中的特定記錄。我的後端是Microsoft Access,前端是Visual Basic。它給了我一個錯誤「沒有給出一個或多個必需參數的值」。另外,我收到一個關於「未將對象引用設置爲對象實例」的問題。VB.NET更新方法
這是我的代碼。謝謝:)
Private Sub UpdateClient()
Dim sqlUpdateClient As String = "UPDATE tblClient SET clientCompany = @clientCompany, clientStreetAddress = @clientStreetAddress, clientCity = @clientCity, clientContactPerson = @clientContactPerson, clientContactNumber = @clientContactNumber, clientEmail = @clientEmail, clientMobileNumber = @clientMobileNumber WHERE clientID = " + selectedClient
Dim recordsAffected As Integer = 0
Dim accessCommand As New OleDbCommand(sqlUpdateClient, accessConnection)
accessCommand.CommandText = sqlUpdateClient
accessCommand.Parameters.AddWithValue("@clientCompany", txtClientCompany.Text)
accessCommand.Parameters.AddWithValue("@clientStreetAddress", txtClientStreetAddress.Text)
accessCommand.Parameters.AddWithValue("@clientCity", txtClientCity.Text)
accessCommand.Parameters.AddWithValue("@clientContactPerson", txtClientContactPerson.Text)
accessCommand.Parameters.AddWithValue("@clientContactNumber", txtClientPhoneNumber.Text)
accessCommand.Parameters.AddWithValue("@clientEmail", txtClientEmailAddress.Text)
accessCommand.Parameters.AddWithValue("@clientMobileNumber", txtClientMobileNumber.Text)
Try
accessConnection.Open()
recordsAffected = accessCommand.ExecuteNonQuery
Catch ex As Exception
lblError.Text = ex.ToString
Finally
accessConnection.Close()
End Try
If recordsAffected = 0 Then
MsgBox("Record updated failed!", MsgBoxStyle.Exclamation, "Project Analysis System")
Else
MsgBox("Record updated successfully!", MsgBoxStyle.Information, "Project Analysis System")
PopulateClientList()
End If
End Sub
哪裏'accessConnection'和'selectedClient'定義,什麼是表'tblClient'的定義是什麼? – vane
你在哪裏收到「對象引用未設置爲對象實例」?另外,爲什麼不分配selectedClient作爲參數@clientID? –
@vane:accessConnection包含我的連接字符串,而selectedClient來自listview,當我從listview中選擇一個項目時,它返回一個整數。 –