Click here to Skip to main content
15,921,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have given code below.
I want To exit the loop after checking the value in database...

public void load_tree()
{
   DataSet PrSet = PDataset("Select * from categories where idparentcategory =1");
   TreeView1.Nodes.Clear();
   foreach (DataRow dr in PrSet .Tables [0].Rows)
   {
      if ((int)dr["idparentcategory"] == 1)
      {
         TreeNode tnParent = new TreeNode();
         tnParent.Text = dr["categoryDesc"].ToString().Trim();
         tnParent.Value = dr["idCategory"].ToString().Trim();
         tnParent.Expand();
         TreeView1.Nodes.Add(tnParent);
         FillChild(tnParent, tnParent.Value);
       }
    }
}

public int FillChild(TreeNode parent, string idCategory)
{
   DataSet ds = PDataset("Select * from categories where idparentcategory =" + idCategory);
   if (ds.Tables[0].Rows.Count > 0)
   {
      foreach (DataRow dr in ds.Tables[0].Rows)
      {
         TreeNode child = new TreeNode();
         child.Text = dr["categoryDesc"].ToString().Trim();
         string temp = dr["idCategory"].ToString();
         child.Collapse();
         //parent.ChildNodes.Add(child);
         //TreeView1.Nodes.Add(child);
         parent.ChildNodes.Add(child);
         FillChild(child, temp);
      }
      return 0;
    }
    else
    {
       return 0;
    }
}
Posted
Updated 8-Mar-11 21:36pm
v3
Comments
Dalek Dave 9-Mar-11 3:37am    
Edited for Readability.

There is a break; key word there for loops. Is that what you mean? However your code is not optimized in such a way querying the database again and again. Find a way to query the required data once and use business logic to fill. Also recursion is costly than for loops.
 
Share this answer
 
Comments
Dalek Dave 9-Mar-11 3:37am    
Good Call.
Albin Abel 9-Mar-11 3:48am    
Thanks Dalek Dave
I'm not exactly sure what you are asking: it can't be as simple as "add a return or a break statement to your code, can it?

If it isn't, what are you trying to do, that is not happening?
 
Share this answer
 

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