deleted row information cannot be accessed...

  • Thread starter Thread starter Co
  • Start date Start date
C

Co

Hi All,

I'm trying to delete multiple files from a database but get an error
sayong deleted row information cannot be accessed through the row.

First I get all the selected files and their IDs.

Case "Delete"
Select Case MsgBox("Bestand Verwijderen..." & _

Microsoft.VisualBasic.ControlChars.Cr & _
"Wil je dit bestand echt
verwijderen?", MsgBoxStyle.YesNo Or MsgBoxStyle.Exclamation,
"Verwijderen")
Case MsgBoxResult.Yes
Dim j As Integer
Dim s As Integer
s = ListView.SelectedItems.Count
Dim aDelFiles(s) As String
For j = 0 To ListView.SelectedItems.Count - 1
aDelFiles(j) = ListView.SelectedItems
(j).Tag
Next
DelFiles(aDelFiles)
Case MsgBoxResult.Cancel
End Select

Then the sub DelFiles will have to do the actual deleting:

Private Sub DelFiles(ByVal aFiles2Delete() As String)

'this code will delete the name of a folder
Dim sql As String = "SELECT * FROM Bestanden"
Dim strTable As String = "Bestanden"
Dim da As New OleDb.OleDbDataAdapter(sql, conn)
Dim cb As New OleDb.OleDbCommandBuilder(da)
Dim ds As New DataSet
Dim i As Integer
Dim dr As DataRow
conn.Open()

cb.QuotePrefix = "["
cb.QuoteSuffix = "]"

Try
da.SelectCommand = New OleDb.OleDbCommand(sql, conn)
da.Fill(ds, strTable)

For i = 0 To aFiles2Delete.Length - 1
For Each dr In ds.Tables(0).Rows
If dr.Item("Id") = aFiles2Delete(i) Then
ds.Tables(strTable).Rows(0).Delete()
End If
Next
Next

'sent the updated dataSet to the database
da.Update(ds, strTable)

Catch oException As OleDbException
MessageBox.Show(oException.Message)

Catch oException As Exception
MessageBox.Show(oException.Message)

End Try
conn.Close()

End Sub

There I get the Exception thrown with the mentioned error message.

Marco
The Netherlands
 
You are attempting to delete the same row over and over:
ds.Tables(strTable).Rows(0).Delete()
Replace that with:
dr.Delete()

Actually you should review your delete logic. The way it is structured for each row you want to
delete you walk the table up to the row. You should put the delete logic in the loop where you walk
the selected items.
 
The Delete method of the DataRow does not remove the row from the Rows collection unless the
RowState is Added. It is safe to walk the Rows collection and execute the Delete method as long as
no rows were added since the last AcceptChanges was executed or since the table was initialized. In
this case, since the table was initialized in the same block of code that the deletes are executed
in there would not be any rows with a RowState of Added. This does not mean it is a good practice
-- only that it won't crash the application.

James Hahn said:
You should not delete items from a collection while you are using For Each
to enumerate the collection. The result is what the error describes - an
attempt to process non-existent items.

Please refer to the "Modifying the Collection" paragraph at the
below-mentioned page:
http://msdn.microsoft.com/en-us/library/5ebk1751.aspx

Co said:
Hi All,

I'm trying to delete multiple files from a database but get an error
sayong deleted row information cannot be accessed through the row.

First I get all the selected files and their IDs.

Case "Delete"
Select Case MsgBox("Bestand Verwijderen..." & _

Microsoft.VisualBasic.ControlChars.Cr & _
"Wil je dit bestand echt
verwijderen?", MsgBoxStyle.YesNo Or MsgBoxStyle.Exclamation,
"Verwijderen")
Case MsgBoxResult.Yes
Dim j As Integer
Dim s As Integer
s = ListView.SelectedItems.Count
Dim aDelFiles(s) As String
For j = 0 To ListView.SelectedItems.Count - 1
aDelFiles(j) = ListView.SelectedItems
(j).Tag
Next
DelFiles(aDelFiles)
Case MsgBoxResult.Cancel
End Select

Then the sub DelFiles will have to do the actual deleting:

Private Sub DelFiles(ByVal aFiles2Delete() As String)

'this code will delete the name of a folder
Dim sql As String = "SELECT * FROM Bestanden"
Dim strTable As String = "Bestanden"
Dim da As New OleDb.OleDbDataAdapter(sql, conn)
Dim cb As New OleDb.OleDbCommandBuilder(da)
Dim ds As New DataSet
Dim i As Integer
Dim dr As DataRow
conn.Open()

cb.QuotePrefix = "["
cb.QuoteSuffix = "]"

Try
da.SelectCommand = New OleDb.OleDbCommand(sql, conn)
da.Fill(ds, strTable)

For i = 0 To aFiles2Delete.Length - 1
For Each dr In ds.Tables(0).Rows
If dr.Item("Id") = aFiles2Delete(i) Then
ds.Tables(strTable).Rows(0).Delete()
End If
Next
Next

'sent the updated dataSet to the database
da.Update(ds, strTable)

Catch oException As OleDbException
MessageBox.Show(oException.Message)

Catch oException As Exception
MessageBox.Show(oException.Message)

End Try
conn.Close()

End Sub

There I get the Exception thrown with the mentioned error message.

Marco
The Netherlands
 
The Delete method of the DataRow does not remove the row from the Rows collection unless the
RowState is Added.  It is safe to walk the Rows collection and execute the Delete method as long as
no rows were added since the last AcceptChanges was executed or since thetable was initialized.  In
this case, since the table was initialized in the same block of code thatthe deletes are executed
in there would not be any rows with a RowState of Added.  This does notmean it is a good practice
-- only that it won't crash the application.

James Hahn said:
You should not delete items from a collection while you are using For Each
to enumerate the collection.  The result is what the error describes -an
attempt to process non-existent items.
Please refer to the "Modifying the Collection" paragraph at the
below-mentioned page:
http://msdn.microsoft.com/en-us/library/5ebk1751.aspx
Co said:
Hi All,
I'm trying to delete multiple files from a database but get an error
sayong deleted row information cannot be accessed through the row.
First I get all the selected files and their IDs.
           Case "Delete"
               Select Case MsgBox("Bestand Verwijderen...." & _
Microsoft.VisualBasic.ControlChars.Cr & _
                                 "Wil je dit bestand echt
verwijderen?", MsgBoxStyle.YesNo Or MsgBoxStyle.Exclamation,
"Verwijderen")
                   Case MsgBoxResult.Yes
                       Dim j As Integer
                       Dim s As Integer
                       s = ListView.SelectedItems.Count
                       Dim aDelFiles(s) As String
                       For j = 0 To ListView..SelectedItems.Count - 1
                           aDelFiles(j) = ListView.SelectedItems
(j).Tag
                       Next
                       DelFiles(aDelFiles)
                   Case MsgBoxResult.Cancel
               End Select
Then the sub DelFiles will have to do the actual deleting:
Private Sub DelFiles(ByVal aFiles2Delete() As String)
       'this code will delete the name of a folder
       Dim sql As String = "SELECT * FROM Bestanden"
       Dim strTable As String = "Bestanden"
       Dim da As New OleDb.OleDbDataAdapter(sql, conn)
       Dim cb As New OleDb.OleDbCommandBuilder(da)
       Dim ds As New DataSet
       Dim i As Integer
       Dim dr As DataRow
       conn.Open()
       cb.QuotePrefix = "["
       cb.QuoteSuffix = "]"
       Try
           da.SelectCommand = New OleDb.OleDbCommand(sql, conn)
           da.Fill(ds, strTable)
           For i = 0 To aFiles2Delete.Length - 1
               For Each dr In ds.Tables(0).Rows
                   If dr.Item("Id") = aFiles2Delete(i) Then
                       ds.Tables(strTable).Rows(0).Delete()
                   End If
               Next
           Next
           'sent the updated dataSet to the database
           da.Update(ds, strTable)
       Catch oException As OleDbException
           MessageBox.Show(oException.Message)
       Catch oException As Exception
           MessageBox.Show(oException.Message)
       End Try
       conn.Close()
   End Sub
There I get the Exception thrown with the mentioned error message.
Marco
The Netherlands

What should I use then instead of For Each loop?
I have tried what Stewart suggested but still get the same error:
dr.Delete()

Marco
 
If you wish to avoid the error you should exit the loop as soon as you have
identified the matching item. However, it would be prefereable to
restructure your code so that you access the item to be deleted directly
rather than working through the collection item by item.


What should I use then instead of For Each loop?
I have tried what Stewart suggested but still get the same error:
dr.Delete()

Marco
 
Back
Top