0

Good day everyone. I would like to ask for help regarding my code to update specific record in the database. My back-end is Microsoft Access and front-end is Visual Basic. It gives me an error "No value given for one or more required parameters". Also, I receive a problem about "object reference not set to an instance of an object."

Here's my code. Thank you :)

    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
4

1 回答 1

1

Is accessConnection a global (connection object) variable? You must have created another instance of that object on that procedure.

One more thing, clientID is also be parameterized. then add this line

accessCommand.Parameters.AddWithValue("@clientID", selectedClient)

UPDATE 1

Private Sub UpdateClient()

    Dim recordsAffected As Integer = 0
    Dim sqlUpdateClient As String = "UPDATE tblClient " & _
                                    "SET clientCompany = ?, " & _
                                    "   clientStreetAddress = ?, " & _
                                    "   clientCity = ?, " & _
                                    "   clientContactPerson = ?, " & _
                                    "   clientContactNumber = ?, " & _
                                    "   clientEmail = ?, " & _
                                    "   clientMobileNumber = ? " & _
                                    "WHERE clientID = ?"

    Using accessConnection As New OleDbConnection("connectionStringHere")
        Using accessCommand As New OleDbCommand()
            With accessCommand
                .Connection = accessConnection
                .CommandType = CommandType.Text
                .CommandText = sqlUpdateClient
                .Parameters.AddWithValue("clientCompany", txtClientCompany.Text)
                .Parameters.AddWithValue("clientStreetAddress", txtClientStreetAddress.Text)
                .Parameters.AddWithValue("clientCity", txtClientCity.Text)
                .Parameters.AddWithValue("clientContactPerson", txtClientContactPerson.Text)
                .Parameters.AddWithValue("clientContactNumber", txtClientPhoneNumber.Text)
                .Parameters.AddWithValue("clientEmail", txtClientEmailAddress.Text)
                .Parameters.AddWithValue("clientMobileNumber", txtClientMobileNumber.Text)
                .Parameters.AddWithValue("clientID", selectedClient)
            End With
            Try
                accessConnection.Open()
                recordsAffected = accessCommand.ExecuteNonQuery()
            Catch ex As OleDBException
                lblError.Text = ex.Message.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 Using
    End Using

End Sub

enter image description here

于 2012-09-05T04:53:14.837 回答