Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Duplicate values in chilnodes when loading treeview

rjn004rjn004 honikPosts: 1

Am populating a treeview from database,i have two tables one-to-many relationship,when am loading it the parent nodes are done but in the child nodes there are duplicate values,like in the first parent node there is street1 and street2 in the second node there is street1 and street2(of the first node) plus its streets 3 and 4 here is my code :
private void PopulateTreeview12()
{
treeView1.Nodes.Clear();
DataSet ds = new DataSet();
DataSet ds2 = new DataSet();

            SqlCommand cmd1 = cnn.CreateCommand();
            cmd1.CommandText = "select areaId, areaName from area";
            SqlDataAdapter sdr1 = new SqlDataAdapter(cmd1);
            cmd1.CommandType = CommandType.Text;
            cmd1.Connection = cnn;
            sdr1.Fill(ds);


            foreach (DataRow item in ds.Tables[0].Rows)
            {
                TreeNode AreaNode = new TreeNode();

                AreaNode.Text = item["areaName"].ToString();
                string areaID = item["areaId"].ToString();

                SqlCommand cmd2 = cnn.CreateCommand();
                cmd2.CommandText = "SELECT streetId, streetName, areaId FROM street WHERE areaId = @areaId";
                cmd2.CommandType = CommandType.Text;
                cmd2.Parameters.AddWithValue("@areaId", areaID);
                SqlDataAdapter sdr2 = new SqlDataAdapter(cmd2);
                cmd2.Connection = cnn;
                sdr2.Fill(ds2);
                foreach (DataRow itemchild in ds2.Tables[0].Rows)
                {
                    TreeNode childtreenode = new TreeNode();
                    childtreenode.Text = itemchild["streetName"].ToString();
                    AreaNode.Nodes.Add(childtreenode);
                }
                treeView1.Nodes.Add(AreaNode);
            }
        }
Tagged:
Sign In or Register to comment.