Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I have the code below whenever I run my application it gives me an error saying "collection was modified."

C#
MyTreeNode Node;

            //-----SYSTEM-----
            //Gets the SYSTEMS into the variable currentSystems
            var currentSystems = DAObject.fetch("SELECT * FROM TSystem").AsEnumerable();
            if (currentSystems.Count() > 0)
                foreach (DataRow systemRow in currentSystems)
                {
                    Node = new MyTreeNode(
                        systemRow["strSystemName"].ToString(),
                        DAObject.GetLinage(systemRow["strFormIdName"].ToString()).ToString(),
                        systemRow["strFormIdName"].ToString()
                        );

                    Node.ForeColor = Color.Green;
                    treeView1.Nodes.Add(Node);

                    //-----SUBSYSTEMS-----
                    //Gets the system SUBSYSTEMS into the variable currentSystemSubSystems
                    var currentSystemSubSystems =
                                from subSystem in (DAObject.fetch("SELECT * FROM TSubSystem").AsEnumerable())
                                where subSystem["nSystemId"] == systemRow["nSystemId"]
                                select subSystem;
                    if (currentSystemSubSystems.Count() > 0)
                        foreach (DataRow subSystemRow in currentSystemSubSystems)
                        {
                            Node = new MyTreeNode(
                                subSystemRow["strSubsystemName"].ToString(),
                                DAObject.GetLinage(subSystemRow["strSubFormID"].ToString()).ToString(),
                                subSystemRow["strSubFormID"].ToString()
                                );

                            Node.ForeColor = Color.Green;
                            treeView1.Nodes.Add(Node);

                            //-----MENUS-----
                            //Gets the Subsystem MENUS into the variable currentSubSystemMenus
                            var currentSubSystemMenus =
                                        from Menu in (DAObject.fetch("SELECT * FROM TMenu").AsEnumerable())
                                        where Menu["nSubsytemId"] == subSystemRow["nSubsytemId"]
                                        select Menu;
                            if (currentSubSystemMenus.Count() > 0)
                                foreach (DataRow menuRow in currentSubSystemMenus) //First time error comes up at this line, when I continue it occurs on other loops 
                                {
                                    Node = new MyTreeNode(
                                        subSystemRow["strMenuname"].ToString(),
                                        DAObject.GetLinage(subSystemRow["strMenuFormID"].ToString()).ToString(),
                                        subSystemRow["strMenuFormID"].ToString()
                                        );

                                    Node.ForeColor = Color.Green;
                                    treeView1.Nodes.Add(Node);


                                    //-----SUBMENUS-----
                                    //Gets the Menu SUBMENUS into the variable currentMenuSubMenus
                                    var currentMenuSubMenus =
                                        from subMenu in (DAObject.fetch("SELECT * FROM TSubMenu").AsEnumerable())
                                        where subMenu["nMenuID"] == menuRow["nMenuID"]
                                        select subMenu;
                                    if (currentMenuSubMenus.Count() > 0)
                                        foreach (DataRow subMenuRow in currentMenuSubMenus)
                                        {
                                            Node = new MyTreeNode(
                                                subSystemRow["strSubMenuName"].ToString(),
                                                DAObject.GetLinage(subSystemRow["strSubMenuFormID"].ToString()).ToString(),
                                                subSystemRow["strSubMenuFormID"].ToString()
                                                );

                                            Node.ForeColor = Color.Green;
                                            treeView1.Nodes.Add(Node);

                                        }
                                    Menu_count += 1;
                                }
                            SubSystem_count += 1;
                        }
                    System_count += 1;

                }


I'm reading it but I con't see what the problem is,
the data tables and data rows are correctly filled and valued.

e1:
After searching for it I saw that looks like the dataTable is removed after the first iteration of the MENU foreach loop. I don't know why.

What I have tried:

I'm still working on it, but yet I couldn't find any solution's for it.
Posted
Updated 5-Sep-16 1:38am
v2
Comments
RickZeeland 3-Sep-16 3:12am    
Try replacing the foreach with a for or loop construction, this might help.
BillWoodruff 3-Sep-16 21:10pm    
Try single-stepping through the code to isolate exactly where the error occurs, and update your question. Assigning a new TreeNode to a TreeNode Field cannot cause the error you report.
m.r.m.40 5-Sep-16 2:13am    
it reads the line,
foreach (var menuRow in currentSubSystemMenus)
for the first time, after reading it and trying to check again it holds on "in" word in the foreach condition. and gives me the error, I cannot see what the problem is.
Karthik_Mahalingam 5-Sep-16 7:00am    
try
foreach (var menuRow in currentSubSystemMenus.ToList())
Maciej Los 5-Sep-16 7:29am    
Do not repost!
http://www.codeproject.com/Questions/1123279/Creating-treenode-dynamicaly-and-recieving-an-erro

1 solution

I'd strongly recommend to read this: Enumerating collections that change in C# | Magnus Montin[^]
In a short: you cannot modify collection inside a forach loop.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900