?
Solved

getting Index was out of range. error  while running a loop for treenode collection

Posted on 2009-02-20
5
Medium Priority
?
1,244 Views
Last Modified: 2012-05-06
I have saved my tree nodes collection in a session object and, i am using that collection to load the tree again.

I am running the below code to add nodes into the tree view control
but i am getting this error when i run this code.
Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index

when i did some testing then i found out the when the nodes are added to the tree view then "nodes.count" is decreasing by1.
and this error comes when the loops run half  +1 time of nodes.count.

any one could help me out here.
TreeNodeCollection nodes = _defaultPageViewState.Nodes; 
            if (nodes == null) { return; }
            for (int i = 0; i <= nodes.Count; i++)  {
                ParentTreeView.Nodes.Add(nodes[i]);
            }

Open in new window

0
Comment
Question by:ziorrinfotech
  • 2
  • 2
5 Comments
 
LVL 4

Expert Comment

by:siancell
ID: 23691602
The for loop should be like this

TreeNodeCollection nodes = _defaultPageViewState.Nodes; 
            if (nodes == null) { return; }
            for (int i = 0; i <= nodes.Count-1; i++)  {
                ParentTreeView.Nodes.Add(nodes[i]);
            }
//OR
 
TreeNodeCollection nodes = _defaultPageViewState.Nodes; 
            if (nodes == null) { return; }
            for (int i = 0; i < nodes.Count-1; i++)  {
                ParentTreeView.Nodes.Add(nodes[i]);
            }

Open in new window

0
 
LVL 4

Expert Comment

by:siancell
ID: 23691607
Sorry, the last code snippet should be like this
TreeNodeCollection nodes = _defaultPageViewState.Nodes; 
            if (nodes == null) { return; }
            for (int i = 0; i < nodes.Count; i++)  {
                ParentTreeView.Nodes.Add(nodes[i]);
            }

Open in new window

0
 
LVL 1

Expert Comment

by:Wizardskills
ID: 23691850
Yep you need the "-1" after the count in the for loop.
0
 
LVL 8

Accepted Solution

by:
tiagosalgado earned 375 total points
ID: 23691902
I don't know if you realy need to set a TreeNodeCollection, but if you not you can do what you want in simply way.

if (_defaultPageViewState.Nodes == null)
    return;
foreach (TreeNode node in _defaultPageViewState.Nodes)
{
    tt.Nodes.Add(node);
}

Open in new window

0
 
LVL 8

Expert Comment

by:tiagosalgado
ID: 23691909
Sorry but change the line inside foreach to this one
 

ParentTreeView.Nodes.Add(node);

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question