Click here to Skip to main content
15,905,875 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#

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 :
C#
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);
            }
        }


Am stuck here, please i need help

What I have tried:

i have tried to use for loop but same result :
for (int i = 0; i < dt.Rows.Count; i++)
{
// Current areaId and areaname is stored
string areaID = dt.Rows[i]["areaId"].ToString();
string areaName = dt.Rows[i]["areaName"].ToString();

TreeNode AreaNode = new TreeNode(areaName);

SqlCommand cmd2 = cnn.CreateCommand();
cmd2.CommandText = "SELECT streetId, streetName FROM street WHERE areaId = @areaId";
cmd2.CommandType = CommandType.Text;
cmd2.Parameters.AddWithValue("@areaId", areaID);
SqlDataAdapter sdr2 = new SqlDataAdapter(cmd2);
cmd2.Connection = cnn;
sdr2.Fill(dt2);

for (int j = 0; j < dt2.Rows.Count; j++)
{

string streetId = dt2.Rows[j]["streetId"].ToString();
string streetName = dt2.Rows[j]["streetName"].ToString();
TreeNode StreetNode = new TreeNode(streetName);
AreaNode.Nodes.Add(StreetNode);
}
treeView1.Nodes.Add(AreaNode);
}
Posted
Updated 3-Jun-16 21:53pm
Comments
Gold$Coin 30-May-16 23:44pm    
why can't you try using ICollection<someclasstype> or IEnumerable<someclasstype>? that will make you life easier in doing such kind of logic.
ramy nemer 31-May-16 10:12am    
well am completely lost have been spending a long time for fixing this issue but dead end ,can u provide me simple code or a reference i can get help please
Gold$Coin 31-May-16 23:34pm    
all you need is to construct a Collection that will help you to populate a tree control, that all rite?
ramy nemer 1-Jun-16 14:32pm    
well thanks, am new to c# and collection i will try to search for it
Herman<T>.Instance 31-May-16 6:48am    
What would happen If you do a cmd2.Parameters.Clear() before the cmd2.Parameters.AddWithValue... ?

The problem is in the small print (bold is from me)
If Fill finds that a primary key exists for a table, it will overwrite data in the DataSet with data from the data source for rows where the primary key column values match those of the row returned from the data source. If no primary key is found, the data is appended to the tables in the DataSet.

So in theory you have two potential solutions - adding a primary key to the street table being one of them. I'll confess that when I tried this it didn't work, and I haven't got time at the moment to work out why.

However, there is an alternative solution, and that is to recreate the dataset ds2 in the inner loop. I.e. move the declaration and initialisation of ds2 to just before you fill it ...
C#
...
DataSet ds2 = new DataSet();
sdr2.Fill(ds2);
...
 
Share this answer
 
i have found my solution here :
i used dt.Clear() before filling.its working well

Quote:
cmd2.Connection = cnn;
dt2.Clear();
sdr2.Fill(dt2);
 
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