Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi..I have at tree view control which retrieve values from database .It retrieve database name according to userid.Here is my snapshot img
Here is my all database names.I want when i click on database name e.g UIIT then its comes blow(subchilds).Please tell me how can i do this.Here is my aspx.cs code :

What I have tried:

aspx.cs code :
C#
public void getdata()
{
string user = Session["name"].ToString();
SqlConnection cnn = new SqlConnection("Data Source=HAMEED_KHAN\\SQLEXPRESS;Initial Catalog=db_compiler;Integrated Security=True");
        SqlCommand cmd2 = new SqlCommand("SELECT User_ID from tbl_user WHERE User_Name='" + user + "'", cnn);
        cnn.Open();
        string id = cmd2.ExecuteScalar().ToString();
        int ID = Int32.Parse(id);
        Session["UID"] = ID;
SqlCommand cmd = new SqlCommand("SELECT Database_Name FROM  Create_db WHERE User_ID='" + ID + "'", cnn);
SqlCommand cmd4 = new SqlCommand("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_CATALOG='UIIT'", cnn);
        DataTable dt = new DataTable();
        DataTable dt2 = new DataTable();
        Master.FindControl("TreeView1");
        Master.FindControl("TreeView8");
        TreeView tv = (TreeView)Master.FindControl("TreeView1");
        TreeView tv8 = (TreeView)Master.FindControl("TreeView8");

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        SqlDataAdapter da2 = new SqlDataAdapter(cmd4);
        da.Fill(dt);
        da2.Fill(dt2);
        cnn.Close();

        var parentNode = new TreeNode("Database");
        var parentNode2 = new TreeNode("Tables");
        if (dt.Rows.Count > 0)
{
  foreach (DataRow row in dt.Rows)
   {
    string value = row["Database_Name"] + "";
     parentNode.ChildNodes.Add(new TreeNode(value));
    parentNode2.ChildNodes.Add(new TreeNode(value));
    }
     foreach (DataRow row2 in dt2.Rows)
     {
      string value2 = row2["TABLE_NAME"] + "";
     parentNode.ChildNodes.Add(new TreeNode(value2));
    }


   // var temp = new TreeNode("More") { NavigateUrl = "~/More_DB.aspx" };
   // parentNode.ChildNodes.Add(temp);
   //  parentNode2.ChildNodes.Add(temp);
    }
       
    tv.Nodes.Add(parentNode);
   tv8.Nodes.Add(parentNode2);
  }

and here is my QUERY to retrieve table name from database,
SQL
SELECT TABLE_NAME
 FROM INFORMATION_SCHEMA.TABLES
 WHERE TABLE_CATALOG='UIIT'

Remember 'UIIT' is just sample.I retrieve table name of every database blow.Just in 'TABLES' column in pic.Thanks
Posted
Comments
Sinisa Hajnal 19-Apr-16 8:05am    
You mean you want to retrieve data about selected node and fill it out on click? IF htat is so, you're looking for BeforeSelect event where you can go to the database and fill selected node with subchildren before selecting it for the user.
BulletVictim 19-Apr-16 8:10am    
Have you tried looping through each node in the treeview and then on that retrieve information dependant on that, then add a child node to the selected node in the loop?
Based on the Image it should be for each childNode in Database. Get the required value for each one then the active node should be SQLDb then attach child node to that node.
Hameed Khan 19-Apr-16 8:17am    
@Sinisa Actually ,These are all database name like UIIT,SQLDB,info,Arid all are database and these database names comes from sql server databaase.I just want when i click on database e.g when click on 'Arid' then blow its tables shown which is related to 'Arid' database
Hameed Khan 19-Apr-16 8:17am    
@BulletVictim Actually ,These are all database name like UIIT,SQLDB,info,Arid all are database and these database names comes from sql server databaase.I just want when i click on database e.g when click on 'Arid' then blow its tables shown which is related to 'Arid' database

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