1

MLM data sample

i have multilevel marketing software data in which a person may join many other person and again joind person will do the same.

i have tried these code

     protected void load_data()
         {

        strQuery = "select * from redjoining where id=995";
        cmd = new SqlCommand(strQuery);
        dt = dbcon.GetData(cmd);

        string cat_code = dt.Rows[0]["id"].ToString();
        string cat_name = dt.Rows[0]["userName"].ToString();

        TreeNode parent = new TreeNode();
        parent.Value = cat_code;
        parent.Text = cat_name;
        TreeView1.Nodes.Add(parent);
        add_childs(parent, cat_code);

    }

now to add child node i use this code but it add only top single record only.

  protected void add_childs(TreeNode tn, string category_code) 
  {

        strQuery = "select * from redjoining where sponserid=@category_code";
        cmd = new SqlCommand(strQuery);
        cmd.Parameters.AddWithValue("@category_code", category_code);
        dt = dbcon.GetData(cmd);

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            string cat_code = dt.Rows[0]["id"].ToString();
            string cat_name = dt.Rows[0]["userName"].ToString();

            TreeNode child = new TreeNode();
            child.Value = cat_code;
            child.Text = cat_name;
            tn.ChildNodes.Add(child);

            add_childs(child, cat_code); //calling the same function
        }
    }
nisarg parekh
  • 413
  • 4
  • 23
Ravi Kumar
  • 11
  • 1

1 Answers1

0

There is an issue in your method to add child records in treeview, check below code

protected void add_childs(TreeNode tn, string category_code) 
  {

        strQuery = "select * from redjoining where sponserid=@category_code";
        cmd = new SqlCommand(strQuery);
        cmd.Parameters.AddWithValue("@category_code", category_code);
        dt = dbcon.GetData(cmd);

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            string cat_code = dt.Rows[i]["id"].ToString();
            string cat_name = dt.Rows[i]["userName"].ToString();

            TreeNode child = new TreeNode();
            child.Value = cat_code;
            child.Text = cat_name;
            tn.ChildNodes.Add(child);

            add_childs(child, cat_code); //calling the same function
        }
    }

In your code you always using dt.Rows[0]["id"].ToString()

use i as your dt have multiple records and you are iterating for loop.

Hope this will help.

Kevin Shah
  • 1,589
  • 1
  • 13
  • 20
  • either use i or 0 in dt.Rows[0]["id"].ToString() it add only first record, because add_childs(child, cat_code); jump from for loop. – Ravi Kumar Jun 10 '19 at 10:17
  • In that case, you need to write a sql query to get all parent and n-level child records. Search for nested CTE for this it will be helpful for you. – Kevin Shah Jun 10 '19 at 10:23