Hello folks,
I have this form which contains datagridviews of two parent child tables. I have this code for deleting the parent and the child rows. The Trips table is the parent and TripDues is the child. I have setup a relation between the two.
When I delete a row from Trips table the first time everything goes fine. But when I delete another row I get the error that DataAdapter.SelectCommand property needs to be initialized. The error occurs on the bold line:
numTripDues = daTripDues.Update(DeletedTripDues)
As I mentioned, this error occurs only when I delete a second row. It works fine the first time. Please help
I have this form which contains datagridviews of two parent child tables. I have this code for deleting the parent and the child rows. The Trips table is the parent and TripDues is the child. I have setup a relation between the two.
When I delete a row from Trips table the first time everything goes fine. But when I delete another row I get the error that DataAdapter.SelectCommand property needs to be initialized. The error occurs on the bold line:
numTripDues = daTripDues.Update(DeletedTripDues)
As I mentioned, this error occurs only when I delete a second row. It works fine the first time. Please help
VB.NET:
Private Sub DeleteButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DeleteButton.Click
If TripsDataGridView.SelectedRows.Count > 0 Then
If MessageBox.Show("Delete Trip? All related TripOrders records will be deleted too!", "Delete Trip", MessageBoxButtons.OKCancel) = DialogResult.OK Then
Try
bsTrips.RemoveCurrent()
UpdateTripAndChildren()
Catch ex As Exception
ds.RejectChanges()
MessageBox.Show(ex.Message)
End Try
End If
Else
MessageBox.Show("No rows in grid or no row selected.")
End If
End Sub
Private Sub UpdateTripAndChildren()
Dim DeletedTrips As DataTable = ds.Tables("Trips").GetChanges(DataRowState.Deleted)
Dim DeletedTripDues As DataTable = ds.Tables("TripDues").GetChanges(DataRowState.Deleted)
Dim numTrips As Integer
Dim numTripDues As Integer
Dim cb As SqlServerCe.SqlCeCommandBuilder
Try
If Not DeletedTripDues Is Nothing Then
cb = New SqlServerCe.SqlCeCommandBuilder(daTripDues)
[B]numTripDues = daTripDues.Update(DeletedTripDues)[/B]
cb.Dispose()
End If
cb = New SqlServerCe.SqlCeCommandBuilder(daTrips)
numTrips = daTrips.Update(DeletedTrips)
cb.Dispose()
ds.AcceptChanges()
MessageBox.Show(numTrips.ToString + " Trip records deleted.")
Catch ex As Exception
MessageBox.Show(ex.Message)
' Update error, resolve and try again
Finally
If Not DeletedTripDues Is Nothing Then
DeletedTripDues.Dispose()
End If
If Not DeletedTrips Is Nothing Then
DeletedTrips.Dispose()
End If
End Try
End Sub