当第二次调用更新例程时,在传递带有已修改行的DataRow集合时,Update需要有效的UpdateCommand

时间:2013-12-31 08:11:00

标签: sql vb.net

这是我在这里发表的第一篇文章,我查看过其他帖子,但找不到足够解决我问题的答案。

我有一个使用SQL数据库的vb.net应用程序,我正在使用一组由SQLDataAdapter填充的表,这一切都正常。我有一个简单的数据网格来显示记录和使用适配器工作将新记录添加到数据表的例程。我有一个例程允许用户通过从数据集中查找记录ID并填充记录视图(文本框和组合框)来编辑记录。当用户更新记录时,它会写入表并在datagrid视图中刷新结果,但如果他们编辑第二条记录,则会在更新dataadapter的行中引发错误。

这是错误: 当传递带有修改行的DataRow集合时,更新需要有效的UpdateCommand。

以下是添加/更新记录时调用的代码提取。

    Private Sub btnSaveRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveRecord.Click
    'Open the connection
    SrvAvConn.Open()

    If boolEditingRecord = False Then
        'Add a new record to the table
        'Create the new row
        Dim newAvailability As DataRow = dsSrvAV.Tables("ServiceAvailability").NewRow()

        'Add some data to it
        newAvailability("Service_ID") = cboServices.SelectedValue
        newAvailability("Date") = Format(dtpDate.Value.ToString, "Short Date")
        newAvailability("Downtime") = nudDowntime.Value
        newAvailability("Notes") = txtNotes.Text
        newAvailability("MajorIncident") = txtMajorIncident.Text
        newAvailability("ActionsTaken") = txtActionsTaken.Text
        newAvailability("Type") = cboType.SelectedValue
        newAvailability("Root_Cause") = txtRootCause.Text

        'Add it to the table
        dsSrvAV.Tables("ServiceAvailability").Rows.Add(newAvailability)
    Else
        'Updating a record
        Dim AvailabilityRow() As Data.DataRow = dsSrvAV.Tables("ServiceAvailability").Select("ID = " & intEditedRecordID)

        AvailabilityRow(0)("Service_ID") = cboServices.SelectedValue
        AvailabilityRow(0)("Date") = Format(dtpDate.Value.ToString, "Short Date")
        AvailabilityRow(0)("Downtime") = nudDowntime.Value
        AvailabilityRow(0)("Notes") = txtNotes.Text
        AvailabilityRow(0)("MajorIncident") = txtMajorIncident.Text
        AvailabilityRow(0)("ActionsTaken") = txtActionsTaken.Text
        AvailabilityRow(0)("Type") = cboType.SelectedValue
        AvailabilityRow(0)("Root_Cause") = txtRootCause.Text
    End If

    'Update the adapter
    daSrvAv.Update(dsSrvAV, "ServiceAvailability")

    'Refresh the data
    dsSrvAV.Tables("AvailabilityHistory").Clear()

    'Populate the history dataset and data grid
    PopulateAvailabilityHistory()

    'Close the connection
    SrvAvConn.Close()

    'Clear the record and set the defaults
    ClearNewRecord()
End Sub

有关信息,所有适配器和数据集都是以编程方式创建的,而不是使用设计器。 任何人都可以帮我找出错误产生的原因吗?提前谢谢。

更新了PopulateAvailabilityHistory代码

   Private Sub PopulateAvailabilityHistory()
    'Populate the availability history
    'Set hourglass on
    Me.Cursor = Cursors.WaitCursor

    'Populate the datagrid with results
    daSrvAv = New SqlDataAdapter("SELECT [ID], IS_Dashboard.dbo.Services.Service as Service ,[Date] ,[Downtime] ,[Notes] ,[MajorIncident] " & _
                                 ",[ActionsTaken] ,IS_Dashboard.dbo.types.Type as 'Type' ,[Root_Cause] " & _
                                 "FROM [IS_Dashboard].[dbo].[ServiceAvailability] INNER JOIN IS_Dashboard.dbo.Types " & _
                                 "ON IS_Dashboard.dbo.ServiceAvailability.type = TypeID " & _
                                 "INNER JOIN IS_Dashboard.dbo.Services ON IS_Dashboard.dbo.ServiceAvailability.Service_ID = " & _
                                 "IS_Dashboard.dbo.Services.Service_ID " & _
                                 "ORDER BY [Date] DESC", SrvAvConn)

    'Load the data into the table
    daSrvAv.SelectCommand.CommandTimeout = intConnTimeout
    daSrvAv.Fill(dsSrvAV, "AvailabilityHistory")

    'Clear any previous result
    DGVAvHist.DataSource = Nothing

    'Populate the results
    DGVAvHist.DataSource = dsSrvAV.Tables("AvailabilityHistory")

    'Set hourglass off
    Me.Cursor = Cursors.Arrow
End Sub

0 个答案:

没有答案