Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

I'm trying to pull the Requirements folder structure from QC using C#. The OTA has a built in treeview for testplans but not for requirements. So far I'm able to pull all the folder under root but all the folders are just children of root. See below

-Requirements
   -folder 1
   -folder 1's child
   -folder 2
   -folder 2's child

What I want is

-Requirements
   -folder 1
      -folder 1's child
   -folder 2
      -folder 2's child

Here is my code

protected void Page_Load(object sender, EventArgs e)
    {
        TreeView1.Nodes.Clear();
        TDConnection td = new TDConnection();
        string serverURL = connectstr;
        td.InitConnectionEx(serverURL);
        td.ConnectProjectEx("test", "test", "test", "test");
        bool checkConnect = td.LoggedIn;
        ReqFactory reqFactory = (ReqFactory)td.ReqFactory;
        HierarchyFilter reqFilter = (HierarchyFilter)reqFactory.Filter;
        reqFilter.KeepHierarchical = true;
        reqFilter["RQ_FATHER_ID"] = "0";
        HierarchySupportList reqs = (HierarchySupportList)reqFilter.NewList();
        TreeNode tn = new TreeNode("Requirements");
        TreeView1.Nodes.Add(tn);
        IterateOverReqs(reqFilter, reqs, tn);

    }

    private void IterateOverReqs(HierarchyFilter reqFilter, HierarchySupportList reqs, TreeNode tn)
    {
        TreeNode t = new TreeNode();
        foreach (Req req in reqs)
        {
            if (req.IsFolder)
            {
                reqFilter["RQ_FATHER_ID"] = "" + req.ID;
                HierarchySupportList childReqs = (HierarchySupportList)reqFilter.NewList();
                t = new TreeNode((string)req.Name);
                tn.ChildNodes.Add(t);
                IterateOverReqs(reqFilter, childReqs, tn);  
            }
        }
share|improve this question
add comment

2 Answers

I think the issue here might be in your recursive call to IterateOverReqs. You pass in the parameter tn which is the top node, therefore, all your requirements are being added to that top node.

Instead, you should pass in the t node so that lower level requirements are added to the node just above it instead of the top node.

private void IterateOverReqs(HierarchyFilter reqFilter, HierarchySupportList reqs, TreeNode tn)
{
    TreeNode t = new TreeNode();
    foreach (Req req in reqs)
    {
        if (req.IsFolder)
        {
            reqFilter["RQ_FATHER_ID"] = "" + req.ID;
            HierarchySupportList childReqs = (HierarchySupportList)reqFilter.NewList();
            t = new TreeNode((string)req.Name);
            tn.ChildNodes.Add(t);

            // Change the below line so that it passes t instead of tn
            IterateOverReqs(reqFilter, childReqs, t);  
        }
    }
}
share|improve this answer
add comment

It looks like your tree creation is correct. So I am assuming it is the tree traversal that you are using that is giving you the incorrect output. You would need to do a pre-order traversal and output that correctly given the tree level you are at.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.