Click here to Skip to main content
15,881,831 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want fill treeview from database by this code dynamically and show in page by read new node data from database and search parent node in treeview and add new node to parent but when i view the page I receive this error

C#
>  Collection was modified; enumeration operation may not execute.
    
   Line 98:                 if (tn.ChildNodes.Count > 0)
    Line 99:                 {
    Line 100:                    foreach (TreeNode cTn in tn.ChildNodes)
    Line 101:                    {
    Line 102:                        int a = SelectChildrenRecursive(cTn, searchValue);

and this is my code :
C#
protected void chartshow_Click(object sender, EventArgs e)
       {
           var query = from k in dbase.chartinsides.AsEnumerable()
                       select k;

           foreach (var item in query)
           {
               if (item.parent == null)
               {
                   TreeNode tn = new TreeNode();
                   tn.Value = item.id.ToString();
                   tn.Text = item.name + " " + item.famili;

                   TreeView1.Nodes.Add(tn);
               }
               else
               {
                   SelectNodesRecursive(item.parent.ToString(), TreeView1,item.name,item.famili,item.id);
               }
           }
       }

       public  void SelectNodesRecursive(string searchValue, TreeView Tv,string name , string famili,int value)
       {
           foreach (TreeNode tn in Tv.Nodes)
           {
               if (tn.Value == searchValue)
               {
                   addnewnode(tn, name+" "+famili,value);
                   break;
               }

               if (tn.ChildNodes.Count > 0)
               {
                   foreach (TreeNode cTn in tn.ChildNodes)
                   {
                       int a = SelectChildrenRecursive(cTn, searchValue);
                       if (a == 1)
                       {

                           addnewnode(tn, charttxtname.Text + " " + charttxtfamili.Text,value);
                       }
                   }
               }
           }
       }
       public static void addnewnode(TreeNode tn, string mytext,int value)
       {
           TreeNode tnt = new TreeNode();
           tnt.Text = mytext;
           tnt.Value = value.ToString();
           tn.ChildNodes.Add(tnt);
       }

       private static int SelectChildrenRecursive(TreeNode tn, string searchValue)
       {
           if (tn.Value == searchValue)
           {
               tn.Expand();
               tn.Select();
               return 1;
           }
           else
           {
               // tn.Parent.Collapse();
               tn.Collapse();
           }
           if (tn.ChildNodes.Count > 0)
           {
               foreach (TreeNode tnC in tn.ChildNodes)
               {
                   int a = SelectChildrenRecursive(tnC, searchValue);
                   if (a == 1)
                   {
                       tn.Expand();
                       return 1;
                   }
               }
           }
           return 0;
       }
Posted
Updated 12-Jul-15 3:20am
v2

1 solution

You cannot modify the collection while looping through it in a foreach loop.

If you need to make modifications, you can use for loop instead, either from 0 to count-1 or vice versa (depending on the code).

From foreach, in (C# Reference)[^]:
The foreach statement is used to iterate through the collection to get the information that you want, but can not be used to add or remove items from the source collection to avoid unpredictable side effects. If you need to add or remove items from the source collection, use a for loop.
 
Share this answer
 
v2

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