Binding Tree View in ASP.NET

Raj.Trivedi
Posted by Raj.Trivedi under ASP.NET category on | Points: 40 | Views : 4131
Hello Bhupender

Check this



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

public partial class TreeView : System.Web.UI.Page
{
string strconn = ConfigurationManager.ConnectionStrings["cnnLocal"].ToString();
SqlConnection sqlconn;
DataSet dstreenode = new DataSet();

//string CategoryID;
TreeNode root = new TreeNode();
TreeNode xnode = new TreeNode();


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindRoot();
}
}

private void BindRoot()
{
sqlconn = new SqlConnection(strconn);
sqlconn.Open();
DataTable dt = RootNodes();
TreeNode headnode = new TreeNode();
headnode.Text = "Root";
//headnode.Value = dt.Rows[0]["CategoryID"].ToString();
xtreeCat.Nodes.Add(headnode);
for (int i = 0; i < dt.Rows.Count; i++)
{
TreeNode firstchild = new TreeNode();
firstchild.Text = dt.Rows[i]["CategoryName"].ToString();
firstchild.Value = dt.Rows[i]["CategoryID"].ToString();
headnode.ChildNodes.Add(firstchild);
DataTable dt1 = ChildNodes(Convert.ToString(firstchild.Value));
for (int j = 0; j < dt1.Rows.Count; j++)
{
TreeNode childnode = new TreeNode();
childnode.Text = dt1.Rows[j]["ImageName"].ToString();
childnode.Value = dt1.Rows[j]["CategoryImageID"].ToString();
firstchild.ChildNodes.Add(childnode);
}

}
}

private DataTable RootNodes()

{
sqlconn = new SqlConnection(strconn);
sqlconn.Open();
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand("select * from CategoriesMaster",sqlconn);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
da.Fill(dt);
return dt;
}


private DataTable ChildNodes(string ParentNode)
{

sqlconn = new SqlConnection(strconn);
sqlconn.Open();
SqlCommand cmd = new SqlCommand("select * from CategoriesImageMaster where CategoryID = '" + ParentNode + "'",sqlconn);
SqlDataAdapter da1= new SqlDataAdapter();
DataTable dt = new DataTable();
da1.SelectCommand = cmd;
da1.Fill(dt);
return dt;
}



}

Comments or Responses

Posted by: Jayakumars on: 4/23/2013 Level:Bronze | Status: [Member] [MVP] | Points: 10
hello raj


where u declare this varibale xtreeCat
Error comes Can u solve this.

Login to post response