Error - Deleted row information cannot be accessed through row

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
 
K

Kevin Yu [MSFT]

Hi Lars,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that when you're trying to update the
deleted rows to a database, an DeletedRowInaccessibleException was thrown.
If there is any misunderstanding, please feel free to let me know.

I tested the code on my machine, however, when updating, it didn't give me
the exception. The DeletedRowInaccessibleException is thrown when trying to
manipulate on a row that has already been deleted. Could you try to step
through the For...Next block before updating? I'm not quite sure what are
the code for in this block. If you comment these out, will the exception
been thrown again? You can also try to set the constraint's
AcceptRejectRule property to none.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
L

larzeb

Kevin,

Thanks for your quick response.

The For..Next block was only inserted after I saw the DataAdapter's Update method failed. I wanted
to make sure that the values I expected to be there were in fact there.

It turns out that the code I posted was OK. The problem was occuring in the RowChanging event of the
DataTable. I was doing some validation in this event and was not checking the RowState before
accessing the table's columns.

Thanks, Lars
 
K

Kevin Yu [MSFT]

You're welcome, Lars.

Thanks for sharing your experience with all the people here. If you have
any questions, please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top