InvalidOperationException  
Author Message
Mule





PostPosted: Visual Basic General, InvalidOperationException Top

Hi ,

I know I am missing the obvious , but why am I getting an InvalidOperationException ,when I try and run this code (vs 2005) , further comment to exception "Collection was modified; enumeration operation might not execute."

Any Solution with examples will be greatly appreciated !

Dim row As HidWkFlwDataSet.tblPodRow

For Each row In Me.HidWkFlwDataSet.tblPod.Rows

If row.Verified = False Then

Me.HidWkFlwDataSet.tblPod.Rows.Remove(row)

End If

Next



Visual Basic22  
 
 
ReneeC





PostPosted: Visual Basic General, InvalidOperationException Top

See if this helps.

Dim row As NEW HidWkFlwDataSet.tblPodRow



 
 
Chuck M





PostPosted: Visual Basic General, InvalidOperationException Top

I think the problem is you're trying to iterate over the same collection your deleting from, using an enumerator. I think for each uses an enumerator, which does not allow insertions or deletions while iterating over the collection. Try using a standard for loop with an index, counting backwards to zero, e.g.

Dim i as Integer

For i = Me.HidWkFlwDataSet.tblPod.Rows.Count - 1 to 0

If Me.HidWkFlwDataSet.tblPod.Rows(i).Verified = False Then

Me.HidWkFlwDataSet.tblPod.Rows.RemoveAt(i)

End If

Next


 
 
Mule





PostPosted: Visual Basic General, InvalidOperationException Top

Thanks Chuck,

I have tried iterating backwards through the collection , the problem is that the code in the If block does not even execute, with the result that the rows are not deleted at all.

Going forward through the collection i.e.

For i = 0 To Me.HidWkFlwDataSet.tblPod.Rows.Count - 1

Results in an IndexOutOfRangeException

hmmm , any ideas


 
 
Chuck M





PostPosted: Visual Basic General, InvalidOperationException Top

I forgot one minor detail...tell the loop to count backwards:

Dim i as Integer

For i = Me.HidWkFlwDataSet.tblPod.Rows.Count - 1 to 0 Step -1

If Me.HidWkFlwDataSet.tblPod.Rows(i).Verified = False Then

Me.HidWkFlwDataSet.tblPod.Rows.RemoveAt(i)

End If

Next i

I think that should fix your problem.

FYI about the other exception:

You’re receiving an IndexOutOfRangeException because you’re removing items from the collection while iterating forward. For example, when I delete the row at index 2, all rows following index 2 have their index change to n – 1 ( row 3 becomes row 2, row 4 becomes row 3, etc.). The for loop starts with the original count, but now the count is decreasing and your indices are changing. When you get to the end of the collection you’re trying to access an invalid index. Iterating backwards preserves your indices.

Also, I think you can use your first solution (foreach) if you use row.Delete(). That modifies the row's state. After the loop you call Me.HidWkFlwDataSet.tblPod.AcceptChanges() and the data table will remove all rows marked for deletion.

Hope this helps.


 
 
Mule





PostPosted: Visual Basic General, InvalidOperationException Top

That did the trick , Chuck thanks for taking the time to respond , it is greatly appreciated.
 
 
ckress-vbstuff





PostPosted: Visual Basic General, InvalidOperationException Top

I found this to be very helpful.

However, the suggestion about using delete and acceptchanges did not work for me. In fact I as you can see from the code below that I commented out, I was just using delete and it was getting the indexoutofrange exception.

So, I'm not sure why. I'm using vb.net 2.0 I don't understand why I got the error. But in my case I am reading a xml file into the dataset object.

Does delete work differently for a xml read: at least that is the way it is acting for me. Maybe I'm missing somthing

Or could you shed some more light on the subject.

But thanks for your code which I am using.

''' <summary>

''' Delete 30 day old entries in order to keep table small. Only if there are more than 30 entries

''' </summary>

''' <remarks></remarks>

Public Sub Delete30DaysOld() Implements _UnitedXMLdb.Delete30DaysOld

'do each table

Dim t As System.Data.DataTable

'reload file

If System.IO.File.Exists(filePath) = True Then

dsxmldb.Clear()

dsxmldb.ReadXml(filePath)

End If

For Each t In dsxmldb.Tables

'For Each r As DataRow In t.Rows

' If DateDiff(DateInterval.Day, r.Item("PaymentDate"), Now) > 30 Then

' r.Delete()

' End If

'Next

Dim i As Integer

For i = t.Rows.Count - 1 To 0 Step -1

If DateDiff(DateInterval.Day, t.Rows.Item(i).Item("PaymentDate"), Now) > 30 Then

t.Rows.Item(i).Delete()

End If

Next

Next

dsxmldb.WriteXml(filePath)

End Sub