views:

456

answers:

2

hi all how r u i faced problem with datatable when i run to client it throw exception and mention the "in" in foreach. Collection was modified; enumeration operation might not execute. this is the code plz help:

foreach (DataRow dr in stStatusTable.Rows)
{
    if (Convert.ToInt32(dr["st_id"]) == stStatus.st_id)
    {
        dr["st_id"] = Convert.ToInt32(dr["st_id"]) + 1;
        dr.AcceptChanges();

    }
    else
    {

        stStatusTable.Rows.Add(stStatus.st_id, stStatus.st_name, stStatus.RTime, stStatus.Total_Mark, stStatus.Completed);
    }
}
+2  A: 

Becouse you are adding rows at the same time you are enumerating them..

You should add the rows to a second clean datatable, and after the loop, copy the rows from the new datatable to the original one.

Burnsys
+1  A: 

Use a for-loop instead of an foreach loop.

However, be careful with the index when adding / removing items.

winSharp93
can u help me for this plz ?
ericx
Where exactly is your problem?
winSharp93
thank u i solve it
ericx