Hi I'm trying to load a treeview from 4 tables in the database, i'm using the code attached.

The problem is that only 2 of the nodes show up, as soon as I get down product level, the node will not show up.

Can anyone see where I went wrong?

private void Load_tree()
      {
         DataSet ds = new DataSet();
         SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CCS_HOConnectionString"].ConnectionString);
         SqlDataAdapter daDepts = new SqlDataAdapter("SELECT * FROM DEPTS", conn);
         SqlDataAdapter daGroups = new SqlDataAdapter("SELECT * FROM PRODGRP", conn);
         SqlDataAdapter daProducts = new SqlDataAdapter("SELECT * FROM PRODUCT", conn);
         SqlDataAdapter daBarcode = new SqlDataAdapter("SELECT * FROM product_barcode", conn);
         daDepts.Fill(ds, "DEPTS");
         daGroups.Fill(ds, "PRODGRP");
         daProducts.Fill(ds, "PRODUCT");
         daBarcode.Fill(ds, "product_barcode");
         ds.Relations.Add("Depts_Group", ds.Tables["DEPTS"].Columns["DEPTCODE"], ds.Tables["PRODGRP"].Columns["PGRPDEPT"]);
         ds.Relations.Add("Group_Product", ds.Tables["PRODGRP"].Columns["PGRPID"], ds.Tables["PRODUCT"].Columns["PRODGRP"]);
         ds.Relations.Add("Product_Barcode", ds.Tables["PRODUCT"].Columns["PRODCODE"], ds.Tables["product_barcode"].Columns["prodcode"]);
         foreach (DataRow dr in ds.Tables["DEPTS"].Rows)
         {
            TreeNode tn = new TreeNode(dr["DEPTSHORT"].ToString());
            foreach (DataRow drGroup in dr.GetChildRows("Depts_Group"))
            {
               TreeNode tnn = new TreeNode(drGroup["PGRPSHORT"].ToString());
               foreach (DataRow drProduct in dr.GetChildRows("Group_Product"))
               {
                  TreeNode tnnn = new TreeNode(drProduct["PRODSHORT"].ToString());
                  foreach (DataRow drBarcode in dr.GetChildRows("Product_Barcode"))
                  {
                     tnnn.Nodes.Add(drBarcode["barcode"].ToString());
                  }
                  tnn.Nodes.Add(tnnn);
               }
               tn.Nodes.Add(tnn);
            }
            trvBarcodes.Nodes.Add(tn);
         }
      }

Maybe you want do this,

foreach (DataRow dr in ds.Tables["DEPTS"].Rows)
         {
            TreeNode tn = new TreeNode(dr["DEPTSHORT"].ToString());
            foreach (DataRow drGroup in dr.GetChildRows("Depts_Group"))
            {
               TreeNode tnn = new TreeNode(drGroup["PGRPSHORT"].ToString());
               foreach (DataRow drProduct in drGroup.GetChildRows("Group_Product"))
               {
                  TreeNode tnnn = new TreeNode(drProduct["PRODSHORT"].ToString());
                  foreach (DataRow drBarcode in drProduct.GetChildRows("Product_Barcode"))
                  {
                     tnnn.Nodes.Add(drBarcode["barcode"].ToString());
                  }
                  tnn.Nodes.Add(tnnn);
               }
               tn.Nodes.Add(tnn);
            }
            trvBarcodes.Nodes.Add(tn);
         }

That was indeed the problem, thank you very much.

I'm glad you got it working. Please mark this thread as solved if you have found an answer to your question and good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.