C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Errors and Solutions » General »

Collection was modified; enumeration operation may not execute


Posted Date: 20 Jun 2009    Resource Type: Errors and Solutions    Category: General
Author: Viji RAJKUMARMember Level: Diamond    
Rating: 1 out of 5Points: 5



The error "Collection was modified; enumeration operation may not execute" is often being experienced by the users.

Whenever the operation of addition of items or deletion of items happening in the list of items using for each loop it happens.

This is because foreach statement uses an Enumerator, which is a little object
optimized to move through a collection which doesn't change.

So if we remove the items from the Enumerator, You are changing the contents of the list during enumeration which is the no-no being reported.

The better way to handle would be to avoid changing such collections.


Consider the following scenario:

We need to remove rows from the DataGridview whose First Field Contains the Value "NA"

Wrong Code:


DataTable dt = (DataTable)GridView1.DataSource;

foreach (DataRow dr in dt.Rows)
{
string dtitem = dr["Item Description"].ToString();

if (dtitem == "NA")
{
dr.Delete();

}

}


dt.AcceptChanges();
gridivew1.DataSource = dt;
gridivew1.DataBind();



We will get the error "Collection was modified; enumeration operation may not execute"


Solution

Here is the work around.




for(int i = dt.Rows.Count - 1; i >= 0; i--)
{
DataRow dr = dt.Rows(i);

string dtitem = dr["Item Description"].ToString();

if (dtitem == item)
{
dt.Rows.Remove(i);
}

}




The trick is in this For Loop only. for(int i = dt.Rows.Count - 1; i >= 0; i--)


Because if we start the index From 0 To dt.Rows.Count - 1

as soon as we remove the rows it cannot recognize the index and will throw "Index Outbound of exception

For Example,

the data table contains 4 Rows.

Assume we start From 0 To 3 and if we remove row 2, the data table contains only 3 rows and will not contain the index 3.



Regards,

Viji Rajkumar



Responses

Author: hitesh panchal    02 Nov 2009Member Level: Bronze   Points : 2
Hi Vijit, your code is perfect. but it had little bit correction as bellows....

for(int i = dt.Rows.Count - 1; i >= 0; i--)
{
DataRow dr = dt.Rows[i]; <-- this line (Bracket)

string dtitem = dr["Item Description"].ToString();

if (dtitem == item)
{
dt.Rows.Remove(dr); <-- this line (dr is replace by i)
}

}

Regards,
Hitesh Panchal


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Enumeration operation may not execute  .  Collection was modified  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: could not load file or assembly 'CrystalDecisions.Windows.Forms, Version=10.2.3600.0, Culture=nutral
Previous Resource: SQL SERVER Error : Windows Update; Error Code 8000FFFF
Return to Discussion Resource Index
Post New Resource
Category: General


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use