Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How can i populate asp:treeview only child populate with database query like "Select Database_Name from database where user_id='1' and show these database in child treeview.Here is imageimage
I want "Database" remain same and blow child "Database Name",,only 1st 3 records replace with "Database Name" shown just,Kindly tell me how can i do this,Thanks.I search on google but i can't find solution.Here is my db pic image

What I have tried:

Try :
C#
  SqlConnection cnn=new SqlConnection("Data Source=HAMEED_KHAN\\SQLEXPRESS;Initial Catalog=db_compiler;Integrated Security=True");

protected void Page_Load(object sender, EventArgs e)
{
}

protected void PopulateTreeView(object sender, EventArgs e)
{   
 SqlCommand cmd=new SqlCommand("SELECT Database_Name FROM  Create_db",cnn);
DataTable dt=new DataTable();
Master.FindControl("TreeView1");
    TreeView tv = (TreeView)Master.FindControl("TreeView1");
TreeNodeCollection parentNode = tv.Nodes;
 SqlDataAdapter da=new SqlDataAdapter(cmd);
 da.Fill(dt);
 PopulateTreeView(parentNode,0, dt);// error come at parent node
} 

Here is my aspx code :
C#
<asp:TreeView ID="TreeView1" runat="server" ImageSet="Arrows" 
                    Font-Size="Larger" onselectednodechanged="TreeView1_SelectedNodeChanged">
                    <HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />
                    <Nodes>
<asp:TreeNode Text="Database" Value="Database">
<asp:TreeNode  Text="Database Name" Value="Database Name"></asp:TreeNode>
<asp:TreeNode Text="Database Name" Value="Database Name"></asp:TreeNode>
<asp:TreeNode Text="Database Name" Value="Database Name"></asp:TreeNode>
<asp:TreeNode Text="More" Value="Database Name"></asp:TreeNode>
</asp:TreeNode>
 </Nodes>
 <NodeStyle Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" 
                        HorizontalPadding="5px" NodeSpacing="0px" VerticalPadding="0px" />
 <ParentNodeStyle Font-Bold="False" />
 <SelectedNodeStyle Font-Underline="True" ForeColor="#5555DD" 
                        HorizontalPadding="0px" VerticalPadding="0px" />
</asp:TreeView>
Posted
Updated 1-Apr-16 21:01pm
Comments
Karthik_Mahalingam 2-Apr-16 2:45am    
what is the error message

1 solution

try this
C#
protected void Page_Load(object sender, EventArgs e)
       {
           if (!Page.IsPostBack)
           {

               SqlCommand cmd = new SqlCommand("SELECT Database_Name FROM  Create_db", cnn);
               DataTable dt = new DataTable();
               Master.FindControl("TreeView1");
               TreeView tv = (TreeView)Master.FindControl("TreeView1");
               SqlDataAdapter da = new SqlDataAdapter(cmd);
               cnn.Open();
               da.Fill(dt);
               cnn.Close();

               var parentNode = new TreeNode("Database");
               if (dt.Rows.Count > 0)
               {
                   foreach (DataRow row in dt.Rows)
                   {
                       string value = row["Database_Name"] + "";
                       parentNode.ChildNodes.Add(new TreeNode(value));
                   }
               }
               tv.Nodes.Add(parentNode);
           }
       }
 
Share this answer
 
v2
Comments
Hameed Khan 2-Apr-16 6:21am    
Thanks for response #KARTHIK ,But soory to say it's not working web page load successfully but nothing happen.
Karthik_Mahalingam 2-Apr-16 6:26am    
did it return any value from db ?
Hameed Khan 2-Apr-16 6:37am    
..
Hameed Khan 2-Apr-16 6:25am    
page load but output not shown
Hameed Khan 2-Apr-16 9:06am    
aweeeeeeeeeeeeeeomee thanksss alot sir thank youuuuu

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