Home - Forums-.NET - FlyTreeView (ASP.NET) - Collection was modified; enumeration operation may not execu

FlyTreeView (ASP.NET)

Technical support and KB related to the FlyTreeView control

This forum related to following products: FlyTreeView for ASP.NET

Collection was modified; enumeration operation may not execu
Link Posted: 16-Feb-2007 04:37
I get the following error \"Collection was modified; enumeration operation may not execute\" right after I do the postback with OnPopulateNodes=\"flyTreeView_PopulateNodes\"  - it does the postback, loops around filling in the new nodes, finishes and then bang.

got any idea why this may be happening.
Link Posted: 16-Feb-2007 06:15
Generally this error occurs when you try to modify collection that is being currently enumerated somewhere.

Could you please post stack trace that you see when getting the error?

Thank you in advance.
Link Posted: 16-Feb-2007 06:25
here you go:

InvalidOperationException: Collection was modified; enumeration operation may not execute.]
   System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) +48
   System.Collections.Generic.Enumerator.MoveNext() +2633777
   NineRays.WebControls.FlyTreeView.PopulateExpandedNodes(IEnumerable`1 nodes) +146
   NineRays.WebControls.FlyTreeView.OnPreRender(EventArgs e) +30
   System.Web.UI.Control.PreRenderRecursiveInternal() +77
   System.Web.UI.Control.PreRenderRecursiveInternal() +161
   System.Web.UI.Control.PreRenderRecursiveInternal() +161
   System.Web.UI.Control.PreRenderRecursiveInternal() +161
   System.Web.UI.Control.PreRenderRecursiveInternal() +161
   System.Web.UI.Control.PreRenderRecursiveInternal() +161
   System.Web.UI.Control.PreRenderRecursiveInternal() +161
   System.Web.UI.Control.PreRenderRecursiveInternal() +161
   System.Web.UI.Control.PreRenderRecursiveInternal() +161
   System.Web.UI.Control.PreRenderRecursiveInternal() +161
   System.Web.UI.Control.PreRenderRecursiveInternal() +161
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1360

The code for handling the populate on demand is the same as in demo_serverevents with a couple of changes to feed my datainto it (instead of filesystem)

thanks
Link Posted: 16-Feb-2007 06:57
It seems that you're modifying collection of e.Node.Parent event argument. Or call e.Node.Remove() or etc to change its container collection. This causes the error.

If possible, could you post your PopulateOnDemand handler.
Link Posted: 16-Feb-2007 08:01
Protected Sub flyTreeView_PopulateNodes(ByVal sender As Object, ByVal e As FlyTreeNodeEventArgs)
      
        AddNodes(e.Node.ChildNodes, e.Node.Path)
    End Sub

    Public Sub AddNodes(ByVal nodes As FlyTreeNodeCollection, ByVal GroupID As Integer)

        tblshowgroups.Visible = True
      

        Dim dt As DataTable
                  dt = cs.Utils.DbHelper.ExecuteDataTable(\"cssp_sit_get_childsites2\", GroupID, 0)
    
      
        For Each group As DataRow In dt.Rows
            Dim ftn As New FlyTreeNode()
                    ftn.Text = HttpUtility.HtmlEncode(group(\"title\"))
                     ftn.Value = group(\"site_id\")
                       flyTreeView.Nodes.Add(ftn)

            Try
                ' do not set populate on demand for nodes that have no nested directories
               If group(\"childnodecount\") > 0 Then
                                      ftn.PopulateNodesOnDemand = True
                End If

            Catch ex As Exception
                                ' add a child node that shows that there was an exception trying to get its child nodes
                AddExceptionNode(ftn.ChildNodes, ex)
            End Try
        Next
         End Sub

thanks for your help
Link Posted: 16-Feb-2007 08:29
Yes, you're using
flyTreeView.Nodes.Add(ftn)

This modifies root nodes collection. You should use
nodes.Add(ftn)
instead.