Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to show all nodes in tree view whatever I enter in database, but it is showing only one node. Only the Top node is showing.

How can I show all the nodes from database?

This is my code:
C#
public partial class treeview : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=........;Initial Catalog=EmployeeDB;Integrated Security=True");
    TreeNode PNode = new TreeNode();
protected void Page_Load(object sender, EventArgs e)
   {
      BindDataInTreeView();
   }

private void BindDataInTreeView()
   {
        SqlConnection con = new SqlConnection("Data Source=..............;Initial Catalog=EmployeeDB;Integrated Security=True");
     
        DataRow dr;
        TreeView1.Nodes.Clear();
        con.Open();
        string qry = "select * from Student1";
        SqlCommand cmd = new SqlCommand(qry,con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        PNode.CollapseAll();
        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i <= dt.Rows.Count-1; i++)
            {
                dr = dt.Rows[i];
                switch (dr["StudentId"].ToString())
                {
                    case "1":
                        PNode = new TreeNode(dr["StudentName"].ToString().Trim());
                        this.TreeView1.Nodes.Add(PNode);
                        break;
                }
                PNode.ExpandAll();
            }
          
        }
    }

protected void btnsave_Click(object sender, EventArgs e)
    {
       con.Open();
       string qry = "insert into Student1 values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')";
          SqlCommand cmd = new SqlCommand(qry,con);
           cmd.ExecuteNonQuery();
           con.Close();
           BindDataInTreeView();
    }
 }
Posted
Updated 26-Mar-12 20:16pm
v2
Comments
André Kraak 27-Mar-12 2:16am    
Edited question:
Added pre tags
Formatted text/code
Spelling/Grammar
Mohamed Mitwalli 27-Mar-12 2:38am    
hi ,
you were saying
"would like to show all nodes in tree view whatever I enter in database"
do u mean like parent and Child ??
senguptaamlan 27-Mar-12 2:41am    
please, explain the requirement in details....with DB table structure...and all the artifacts....
Mohamed Mitwalli 27-Mar-12 2:52am    
you want show all student on the DB right Coz this code only show the First one right

1 solution

If i understand you right this would be ur Solution
C#
SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=test;Integrated Security=True");
    TreeNode PNode = new TreeNode();
    protected void Page_Load(object sender, EventArgs e)
    {
        BindDataInTreeView();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        con.Open();
        string qry = "insert into Student1 values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')";
        SqlCommand cmd = new SqlCommand(qry, con);
        cmd.ExecuteNonQuery();
        con.Close();
        BindDataInTreeView();
    }

    private void BindDataInTreeView()
    {
        //SqlConnection con = new SqlConnection("Data Source=..............;Initial Catalog=EmployeeDB;Integrated Security=True");

        DataRow dr;
        TreeView1.Nodes.Clear();
        con.Open();
        string qry = "select * from Student1";
        SqlCommand cmd = new SqlCommand(qry, con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        PNode.CollapseAll();
        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i <= dt.Rows.Count - 1; i++)
            {
                 dr = dt.Rows[i];
                //switch (dr["StudentId"].ToString())
                //{
                //    case "1":
                //        PNode = new TreeNode(dr["StudentName"].ToString().Trim());
                //        this.TreeView1.Nodes.Add(PNode);
                //        break;


                 PNode = new TreeNode(dr["StudentName"].ToString().Trim());
                 this.TreeView1.Nodes.Add(PNode);
                PNode.ExpandAll();
            }

        }
    }
 
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