Home > ASP.Net > Bind TreeView from Database using ADO.Net

Bind TreeView from Database using ADO.Net

Hi all,

try this example to Bind TreeView from Database using ADO.Net

1) Open MS SQL Server 2005 and create new Database

2) Add two Table ParentTable and ChildTable with the following coloumns as below



3) Open MS VS 2008 and create a new website and add new web page

4) From ToolBox add TreeView control to your page and in code behind add the following code:

protected void Page_Load(object sender, EventArgs e)

public void fillParent()
DataSet PrSet = PDataset("Select * from ParentTable");
foreach (DataRow dr in PrSet.Tables[0].Rows)
TreeNode tnParent = new TreeNode();
tnParent.Text = dr["ParentName"].ToString();
tnParent.Value = dr["ParentID"].ToString();
tnParent.PopulateOnDemand = true;
tnParent.ToolTip = "Click to get Child";
tnParent.SelectAction = TreeNodeSelectAction.SelectExpand;
tnParent.Selected = true;
FillChild(tnParent, tnParent.Value);

public void FillChild(TreeNode parent,string ParentId)
DataSet ds = PDataset("Select * from ChildTable where ParentId =" + ParentId);
foreach (DataRow dr in ds.Tables[0].Rows)
TreeNode child = new TreeNode();
child.Text = dr["ChildName"].ToString().Trim();
child.Value = dr["ChildId"].ToString().Trim();
if (child.ChildNodes.Count == 0)
child.PopulateOnDemand = true;
child.ToolTip = "Click to get Child";
child.SelectAction = TreeNodeSelectAction.SelectExpand;

protected DataSet PDataset(string Select_Statement)
SqlConnection SqlCon = new SqlConnection(ConnectionString here);
SqlDataAdapter ad = new SqlDataAdapter(Select_Statement, SqlCon);
DataSet ds = new DataSet();
return ds;

after that open your page it will be like this after binding data


Hope this helps

Good Luck.

Categories: ASP.Net
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: