L
larzeb
I am using a dataset containing Parent/Child tables. When I attempt to delete
more than a single row, I get the DeletedRowInaccessibleException error on the
second row to be deleted. I cannot figure out why.
I have looked at the data just before the failed call (identified by ==>). I have
verified that the tblDetail.Select method creates a datarow array with the
correct number of rows deleted and the data in those rows represent the rows
which I deleted (in a grid).
The Insert and Update sql commands work as expected. The snippet follows.
I left in the code which helped me verify that the deleted data was correct.
The method creating the DataAdapter's DeleteCommand is at the bottom.
Thanks for any help, Lars
====================================================
Dim cn As New SqlConnection(ConnectionSettings.cnString)
Dim daMaster As New SqlDataAdapter("usp_Master_Sel_All", cn)
Dim daDetail As New SqlDataAdapter("usp_MasterDetail_Sel_All", cn)
Dim tblMaster As DataTable = ds.Tables(0)
Dim tblDetail As DataTable = ds.Tables(1)
daMaster.InsertCommand = Me.CreateInsertMaster(cn)
daMaster.UpdateCommand = Me.CreateUpdateMaster(cn)
daMaster.DeleteCommand = Me.CreateDeleteMaster(cn)
daDetail.InsertCommand = Me.CreateInsertDetail(cn)
daDetail.UpdateCommand = Me.CreateUpdateDetail(cn)
daDetail.DeleteCommand = Me.CreateDeleteDetail(cn)
cn.Open()
Try
' Submit the only new Master/Detail rows
daMaster.Update(tblMaster.Select("", "", DataViewRowState.Added))
daDetail.Update(tblDetail.Select("", "", DataViewRowState.Added))
' Submit the only modified Master/Detail rows
daMaster.Update(tblMaster.Select("", "", DataViewRowState.ModifiedCurrent))
daDetail.Update(tblDetail.Select("", "", DataViewRowState.ModifiedCurrent))
' Submit only deleted Detail then Master rows
Dim strx As String
Dim dr() As DataRow = tblDetail.Select("", "", DataViewRowState.Deleted)
Dim num As Integer = dr.Length()
For Each rw As DataRow In dr
For Each col As DataColumn In tblDetail.Columns
If Not rw.IsNull(col, DataRowVersion.Original) Then
strx = rw.Item(col, DataRowVersion.Original)
End If
Next
Next
==>daDetail.Update(tblDetail.Select("", "", DataViewRowState.Deleted))
daMaster.Update(tblMaster.Select("", "", DataViewRowState.Deleted))
Catch ex As SqlException
MessageBox.Show(Exceptions.HandleError(ex))
End Try
cn.Close()
Private Function CreateDeleteDetail(ByVal cn As SqlConnection) As SqlCommand
Dim cmd As New SqlCommand("usp_MC_Del", cn)
cmd.CommandType = CommandType.StoredProcedure
Dim parm As SqlParameter
Dim pc As SqlParameterCollection = cmd.Parameters
parm = pc.Add("@MCID", SqlDbType.Int, 0, "MCID")
parm.SourceVersion = DataRowVersion.Original
Return cmd
End Function
more than a single row, I get the DeletedRowInaccessibleException error on the
second row to be deleted. I cannot figure out why.
I have looked at the data just before the failed call (identified by ==>). I have
verified that the tblDetail.Select method creates a datarow array with the
correct number of rows deleted and the data in those rows represent the rows
which I deleted (in a grid).
The Insert and Update sql commands work as expected. The snippet follows.
I left in the code which helped me verify that the deleted data was correct.
The method creating the DataAdapter's DeleteCommand is at the bottom.
Thanks for any help, Lars
====================================================
Dim cn As New SqlConnection(ConnectionSettings.cnString)
Dim daMaster As New SqlDataAdapter("usp_Master_Sel_All", cn)
Dim daDetail As New SqlDataAdapter("usp_MasterDetail_Sel_All", cn)
Dim tblMaster As DataTable = ds.Tables(0)
Dim tblDetail As DataTable = ds.Tables(1)
daMaster.InsertCommand = Me.CreateInsertMaster(cn)
daMaster.UpdateCommand = Me.CreateUpdateMaster(cn)
daMaster.DeleteCommand = Me.CreateDeleteMaster(cn)
daDetail.InsertCommand = Me.CreateInsertDetail(cn)
daDetail.UpdateCommand = Me.CreateUpdateDetail(cn)
daDetail.DeleteCommand = Me.CreateDeleteDetail(cn)
cn.Open()
Try
' Submit the only new Master/Detail rows
daMaster.Update(tblMaster.Select("", "", DataViewRowState.Added))
daDetail.Update(tblDetail.Select("", "", DataViewRowState.Added))
' Submit the only modified Master/Detail rows
daMaster.Update(tblMaster.Select("", "", DataViewRowState.ModifiedCurrent))
daDetail.Update(tblDetail.Select("", "", DataViewRowState.ModifiedCurrent))
' Submit only deleted Detail then Master rows
Dim strx As String
Dim dr() As DataRow = tblDetail.Select("", "", DataViewRowState.Deleted)
Dim num As Integer = dr.Length()
For Each rw As DataRow In dr
For Each col As DataColumn In tblDetail.Columns
If Not rw.IsNull(col, DataRowVersion.Original) Then
strx = rw.Item(col, DataRowVersion.Original)
End If
Next
Next
==>daDetail.Update(tblDetail.Select("", "", DataViewRowState.Deleted))
daMaster.Update(tblMaster.Select("", "", DataViewRowState.Deleted))
Catch ex As SqlException
MessageBox.Show(Exceptions.HandleError(ex))
End Try
cn.Close()
Private Function CreateDeleteDetail(ByVal cn As SqlConnection) As SqlCommand
Dim cmd As New SqlCommand("usp_MC_Del", cn)
cmd.CommandType = CommandType.StoredProcedure
Dim parm As SqlParameter
Dim pc As SqlParameterCollection = cmd.Parameters
parm = pc.Add("@MCID", SqlDbType.Int, 0, "MCID")
parm.SourceVersion = DataRowVersion.Original
Return cmd
End Function