Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am designing a treeview in VS Webform by retrieving data from msaccess. But unable to do it.

I am having a table with three columns

Id Name ManagerID
1 A 0
2 B 1
3 C 1
4 D 2
5 E 3

First created parent node with managerid 0. Later searched for all names having managerid 1 (B and C) and was able to added to parent node A.

But when tried to search node with Name "B"
treeView1.Nodes.Find(srtext,true)

No nodes are retrieved.

Please help in this.

C#
for (int i = 0; i < intDepth; i++) 
{ 
   query = @"SELECT ID,EMPNAME,MANAGERID FROM EMPLOYEE where ManagerId = " + i.ToString(); 
   myCommand = new OleDbCommand(query, myConnection); 
   OleDbDataReader reader = myCommand.ExecuteReader(); 
   if (i == 0) 
   {
     while (reader.Read()) 
     { 
       parentNode = new TreeNode(reader["EMPNAME"].ToString()); 
       parentNode.Tag = reader["ID"]; 
     } 
     treeView1.Nodes.Add(parentNode); 
   } 
   else 
   { 
     query = @"SELECT ID,EMPNAME,MANAGERID FROM EMPLOYEE where ID = " + i.ToString(); 
     myCommand = new OleDbCommand(query, myConnection); 
     OleDbDataReader reader1 = myCommand.ExecuteReader(); 
     while (reader1.Read() && i != 1) 
     { 
       parentNode = find(reader1["EMPNAME"].ToString()); 
     } 
     while (reader.Read()) 
     { 
       parentNode.Nodes.Add(reader["EMPNAME"].ToString()); 
     } 
}



Thanks in advance

Govardhan
Posted
Updated 8-Aug-11 5:19am
v3
Comments
Toniyo Jackson 8-Aug-11 7:48am    
Share your code also
govardhan4u 8-Aug-11 7:50am    
for (int i = 0; i < intDepth; i++)
{
query = @"SELECT ID,EMPNAME,MANAGERID FROM EMPLOYEE where ManagerId = " + i.ToString();
myCommand = new OleDbCommand(query, myConnection);
OleDbDataReader reader = myCommand.ExecuteReader();
if (i == 0)
{
while (reader.Read())
{


parentNode = new TreeNode(reader["EMPNAME"].ToString());
parentNode.Tag = reader["ID"];

}
treeView1.Nodes.Add(parentNode);
}
else
{
query = @"SELECT ID,EMPNAME,MANAGERID FROM EMPLOYEE where ID = " + i.ToString();
myCommand = new OleDbCommand(query, myConnection);
OleDbDataReader reader1 = myCommand.ExecuteReader();
while (reader1.Read() && i != 1)
{
parentNode = find(reader1["EMPNAME"].ToString());

}

while (reader.Read())
{
parentNode.Nodes.Add(reader["EMPNAME"].ToString());
}

}
govardhan4u 8-Aug-11 7:51am    
private TreeNode find(string srtext)
{
foreach (TreeNode tn in treeView1.Nodes.Find(srtext,true))
{
if (tn.Text == srtext)
{
return tn;
}
}
return null;
}
Kim Togo 8-Aug-11 11:26am    
@govardhan4u.
Please use "Add Comment" and not "Add solution".

1 solution

Nodes.Find searches for the node key and not the node text. Therefore, when you add a node, be sure to specify its key also, like this:
C#
Nodes.Add("B", "B");

And then Nodes.Find will search the node for you.
 
Share this answer
 
Comments
govardhan4u 8-Aug-11 7:52am    
ok let me try.

Thanks
govardhan4u 8-Aug-11 8:27am    
I tried to do like this:
treeView1.Nodes[reader1["EMPNAME"].ToString()].Nodes.Add(reader["EMPNAME"].ToString(), reader["EMPNAME"].ToString());

where reader1 is pointing to parent and reader is child.

but getting error : Object reference not set to an instance of an object.
Kim Togo 8-Aug-11 11:26am    
From OP:

The issue is solved.

Thanks a lot

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