2

I have a System.Windows.Forms.Treeview control with check boxes to build a permission tree, the nodes are dynamically built from DB Records along with custom nodes added from code behind Allow Add, Allow Delete, Allow Update.

The problem is when I try to retrieve the check nodes from the tree I get a StackOverflowException.

Here is my code:

List<tbl_JobPermission> SaveCheckedPermissions(int _JobID, System.Windows.Forms.TreeNodeCollection theNodes)
{
    using (WFMDBEntities _DBContext = new WFMDBEntities())
    {
        tbl_JobPermission _JopPermissionHelperVar;
        if (theNodes.Count > 0)
        {
            foreach (System.Windows.Forms.TreeNode aNode in theNodes.OfType<System.Windows.Forms.TreeNode>().Where(x => x.Checked == true))
            {
                int _tempJobPermID = int.Parse(aNode.Parent.Name);
                if (aNode.Name.Contains('_'))
                {
                    _JopPermissionHelperVar =
                            new tbl_JobPermission()
                            {
                                TblPremition = _tempJobPermID,
                                Tbljob = _JobID
                            };
                    if (aNode.Name.ToLower().Contains("add"))
                    {
                        _JopPermissionHelperVar.AllowNew = true;
                    }
                    else if (aNode.Name.ToLower().Contains("update"))
                    {
                        _JopPermissionHelperVar.AllowUpdate = true;
                    }
                    else if (aNode.Name.ToLower().Contains("delete"))
                    {
                        _JopPermissionHelperVar.AllowDelete = true;
                    }

                    if (!_JobPermissions.Contains(_JopPermissionHelperVar))
                    {
                        _JobPermissions.Add(_JopPermissionHelperVar);
                    }
                }
                else
                {
                    _JopPermissionHelperVar =
                            new tbl_JobPermission()
                            {
                                TblPremition = int.Parse(aNode.Name),
                                Tbljob = _JobID
                            };
                    if (!_JobPermissions.Contains(_JopPermissionHelperVar))
                    {
                        _JobPermissions.Add(_JopPermissionHelperVar);
                    }
                }
                if (aNode.Nodes.Count > 0)
                {
                    _JobPermissions.AddRange(SaveCheckedPermissions(_JobID, aNode.Nodes));
                }
            }
        }
    }
    return _JobPermissions;
}
3
  • 1
    Any chance for same node appearing more than once in the tree? Does each node have some unique identifier? Commented Mar 19, 2013 at 15:32
  • see if you are doing it msdn.microsoft.com/en-us/library/wwc698z7.aspx Commented Mar 19, 2013 at 15:43
  • @ShadowWizard each and every Node has a unique name Commented Mar 19, 2013 at 16:36

2 Answers 2

1

You have a recursive call here:

if (aNode.Nodes.Count > 0)
{
    _JobPermissions.AddRange(SaveCheckedPermissions(_JobID, aNode.Nodes));
}

Check that you have reduced the number of nodes before recursing by running under the debugger.

I think that if all the nodes are checked at a particular level, it will recurse infinitely.

[EDIT]

It could just be that the node levels are too many. How many levels of recursion does it get to before crashing? Can you instrument it with Debug.WriteLines?

Sign up to request clarification or add additional context in comments.

3 Comments

Well I Use the recursive call to Traverse through the children of the current Node, otherwise I will only get the root nodes, and also I check for the count if (aNode.Nodes.Count > 0) here to know if the current node has children of not
The code is fairly complex, so I think the only way to discover what's going on is to single step it in the debugger.
I think I solved the recursion issue, I will post the solution soon enough :)
1

I figured out why I got the StackOverFlow exception, the problem was not in calling the method recursively it was in the return, I was returning a List of the data acquired each time I call the method, so the List items reached a point where they doubles drastically, so what I did was simple ==> I made the method return type Void not a list [ Because I will be filling that List(which is declared in class scope)

void SaveCheckedPermissions(int _JobID, System.Windows.Forms.TreeNode RootNode)
{
    using (WFMDBEntities _DBContext = new WFMDBEntities())
    {
        tbl_JobPermission _JopPermissionHelperVar;

        foreach (System.Windows.Forms.TreeNode aNode in RootNode.Nodes)
        {
            if (aNode.Checked == true)
            {
                int _tempJobPermID;
                int.TryParse(aNode.Name, out _tempJobPermID);
                _JopPermissionHelperVar = new tbl_JobPermission();
                _JopPermissionHelperVar.Tbljob = _JobID;
                if (aNode.Name.Contains('_'))
                {
                    int _tempSpecialJobPermID;
                    int.TryParse(aNode.Parent.Name, out _tempSpecialJobPermID);
                    if (_JobPermissions.Where(x => x.TblPremition == _tempSpecialJobPermID).FirstOrDefault() != null)
                    {
                        _JobPermissions.Where(x => x.TblPremition == _tempSpecialJobPermID).FirstOrDefault().TblPremition = _tempSpecialJobPermID;
                        if (aNode.Name.ToLower().Contains("add"))
                        {
                            _JobPermissions.Where(x => x.TblPremition == _tempSpecialJobPermID).FirstOrDefault().AllowNew = true;
                        }
                        else if (aNode.Name.ToLower().Contains("update"))
                        {
                            _JobPermissions.Where(x => x.TblPremition == _tempSpecialJobPermID).FirstOrDefault().AllowUpdate = true;
                        }
                        else if (aNode.Name.ToLower().Contains("delete"))
                        {
                            _JobPermissions.Where(x => x.TblPremition == _tempSpecialJobPermID).FirstOrDefault().AllowDelete = true;
                        }
                    }
                }
                else
                {
                    if (_JobPermissions.Where(x => x.TblPremition == _tempJobPermID).FirstOrDefault() == null)
                    {
                        _JopPermissionHelperVar.TblPremition = _tempJobPermID;
                    }

                }

                if (_JobPermissions.Where(x => x.TblPremition == _JopPermissionHelperVar.TblPremition && x.Tbljob == _JopPermissionHelperVar.Tbljob).ToList().Count < 1)
                {
                    _JobPermissions.Add(_JopPermissionHelperVar);
                }
            }
            SaveCheckedPermissions(_JobID, aNode);
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.