2012-09-05 67 views
0

大家好日子。我想請求幫助我的代碼更新數據庫中的特定記錄。我的後端是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 
+1

哪裏'accessConnection'和'selectedClient'定義,什麼是表'tblClient'的定義是什麼? – vane

+0

你在哪裏收到「對象引用未設置爲對象實例」?另外,爲什麼不分配selectedClient作爲參數@clientID? –

+0

@vane:accessConnection包含我的連接字符串,而selectedClient來自listview,當我從listview中選擇一個項目時,它返回一個整數。 –

回答

1

accessConnection全球(連接對象)變量?您必須在該過程中創建該對象的另一個實例。

還有一件事,clientID也被參數化。然後加入這一行

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

更新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

+0

@JohnWoo clientID不需要參數化;這是最好的練習,但不會將其作爲參數並將其連接起來,就像他正在做的那樣。 – vane

+0

@vane當你用其他值提供參數而有些不提供參數時,它很奇怪。無論如何,這是用戶的選擇。 –

+0

@JohnWoo我完全同意,並認爲它應該參數化,我只是不想錯誤信息浮動,因爲你說它**必須**參數化,這種說法是錯誤的。 – vane