2

Hi I'm sorry I am a beginner at C# programming and although I know a lot of threads has been created on this issue, I can't seem to find one that is applicable to my case especially since I already applied this on other forms and it worked.

This solution tells me iterate it backwards, however I don't know how/where to apply it. I am getting the error at the in in

foreach (DataRow dataRow_AvailableEmp in AvailableEmp_dataTable.Rows)

here's the rest of the code.

    if (WithTasks_Datatable.Rows.Count > 0)
    {
        foreach (DataRow dataRow_WithTask in WithTasks_Datatable.Rows)
        {
            Booked_Initial = dataRow_WithTask["Assigned"].ToString();
            if (this_Initial != Booked_Initial) //di booked
            {
                //CHECK IF IN DATATABLE                                
                if (AvailableEmp_dataTable.Rows.Count > 0)
                {
                    foreach (DataRow dataRow_AvailableEmp in AvailableEmp_dataTable.Rows)
                    {
                        Available_Initial = dataRow_AvailableEmp["EmpID"].ToString();

                        if (this_Initial != Available_Initial) //NOT IN DB
                        {
                            //ADD

                            AvailableEmp_dataTable.Rows.Add(this_EmpID, this_Initial, PositionName, TeamName, TeamLead);
                        }
                    }
                }
                else
                {
                    //ADD
                    AvailableEmp_dataTable.Rows.Add(this_EmpID, this_Initial, PositionName, TeamName, TeamLead);
                }
            }
        }
    }
Community
  • 1
  • 1
Ritzel
  • 33
  • 2
  • 4
  • 9
  • possible duplicate of [Help with "Collection was modified; enumeration operation might not execute."](http://stackoverflow.com/questions/7109806/help-with-collection-was-modified-enumeration-operation-might-not-execute) – David Jan 10 '12 at 16:21
  • @David If I were to apply a for loop, how will I be able to assign a name to the datatable so I can get the value? Sorry, I have not tried using for before – Ritzel Jan 10 '12 at 16:23

5 Answers5

8

One simple solution is to put the rows you are iterating into a new list:

foreach (DataRow dataRow_AvailableEmp in AvailableEmp_dataTable.Rows.Cast<DataRow>().ToList())
Magnus
  • 45,362
  • 8
  • 80
  • 118
1

You can't / shouldn't modify a collection accessed through an enumerator.

Basically Enumerators are find previous or next record, if you makea change to the collection which changes what the next or previous will be....

Two attacks for this access by index instead as suggested by @mhornfeck

Or you create an empty datatable(s), add to them. Then append the added rows to the original collection.

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39
0

To iterate backwards, use for instead of foreach:

for (int i = AvailableEmp_dataTable.Rows.Count - 1; i >= 0; --i)
        {
            DataRow dataRow_AvailableEmp = AvailableEmp_dataTable.Rows[i];
            Available_Initial = dataRow_AvailableEmp["EmpID"].ToString();

            if (this_Initial != Available_Initial) //NOT IN DB
            {
                //ADD
                AvailableEmp_dataTable.Rows.Add(this_EmpID, this_Initial, PositionName, TeamName, TeamLead);
            }
        }

From http://www.dotnetperls.com/foreach

The foreach loop construct provides a way to elegantly loop through elements, but it has the drawback of restricting any mutations made to the collection during the loop.

Michael Hornfeck
  • 1,242
  • 1
  • 16
  • 33
0

If you wanna use foreach (no reason you should), you will have to loop once to create a list of all the employees you wanna add. Iterate over the list of employees to add and add them to the database.

Bassam Mehanni
  • 14,796
  • 2
  • 33
  • 41
0

That solution applies to removing items from a collection and is used to keep your iteration variable from skipping over items after an item in the middle is deleted (sort of like running away from a crumbling cliffside. It will not work for adding items (nor will iterating forwards) as your Count will change periodically.

To iterate through the collection and add items based on what you find, add the items to a temporary List<DataRow> and then add them to the DataTable after the iteration is complete.

D Stanley
  • 149,601
  • 11
  • 178
  • 240