PC Review


Reply
Thread Tools Rate Thread

Error when delete a row

 
 
Nguyen
Guest
Posts: n/a
 
      15th Mar 2004
Do you know what's wrong with the code below? I get an error: error in
method ~MoveNext: collection was modified. Enumeration operation may not
execute. It happens when I step through the 'Next' of for-loop

Thanks in advance,

V.

-----------------------------------

Dim dtRow As DataRow

With ds.Tables(0)

For Each dtRow In .Rows

If dtRow.IsNull("POPLINE") Then

dtRow.Delete()

End If

Next

End With


 
Reply With Quote
 
 
 
 
Peter Borremans
Guest
Posts: n/a
 
      15th Mar 2004
Nguyen,

It's not allowed to change the iteration variable of a for each loop.
In this case 'dtRow'!
When you call dt.Row.Delete() you attempt to change this variable...

You can easily verify this beheaviour by typing this into your compiler:
(you can change it to VB...)

int[] intArray = {1, 2, 3, 4}; //Array of integers

foreach(int i in intArray)
{
i = 5;
}

The above code will not compile!!

I hope this helps...

Peter

"Nguyen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Do you know what's wrong with the code below? I get an error: error in
> method ~MoveNext: collection was modified. Enumeration operation may not
> execute. It happens when I step through the 'Next' of for-loop
>
> Thanks in advance,
>
> V.
>
> -----------------------------------
>
> Dim dtRow As DataRow
>
> With ds.Tables(0)
>
> For Each dtRow In .Rows
>
> If dtRow.IsNull("POPLINE") Then
>
> dtRow.Delete()
>
> End If
>
> Next
>
> End With
>
>
>




 
Reply With Quote
 
Miha Markic [MVP C#]
Guest
Posts: n/a
 
      16th Mar 2004
Hi,

Your code will work only sometimes.
It will work on rows that are not added (when Deleting those rows, only
their RowStatus is changed) but it won't work on rows that are added
(Deleting those rows will remove them) because of nature of foreach.
Instead, do the inverse loop
Dim i as integer
For i = .Rows-1 to 0 step -1
...
next

"Nguyen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Do you know what's wrong with the code below? I get an error: error in
> method ~MoveNext: collection was modified. Enumeration operation may not
> execute. It happens when I step through the 'Next' of for-loop
>
> Thanks in advance,
>
> V.
>
> -----------------------------------
>
> Dim dtRow As DataRow
>
> With ds.Tables(0)
>
> For Each dtRow In .Rows
>
> If dtRow.IsNull("POPLINE") Then
>
> dtRow.Delete()
>
> End If
>
> Next
>
> End With
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
http://LongPathTool.com - Fixer for "Cannot delete file" error Buthow does the Long Path Tool aid? It is able to rename, copy, and delete filesregardless from the error message displayed. The most typical of that is thepath too extended er Martin Krag Windows XP Accessibility 0 12th Jun 2011 02:19 PM
Delete query - delete from specified tables error klkropf Microsoft Access 4 3rd Feb 2010 11:56 PM
Delete Query Error "Can't delete from specified tables Maureen227 Microsoft Access Queries 4 26th Jul 2006 05:58 PM
Delete Query Error "Can't delete from specified tables Maureen227 Microsoft Access 6 26th Jul 2006 04:36 PM
how to delete a delete error message weldev@yahoo.com Windows XP General 3 7th Mar 2004 12:10 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:22 PM.