In this article we will bind three SQL Server tables with a TreeView in a hierarchical order.
Objective
In this article we will populate a TreeView with three tables - Country, State, and City in a hierarchical order. Country is at the top level which contains its states which further contains cities. The final output will be like in the following figure:
Step 1:
First of all create three tables, Country, State, and City with the following specification and some data like above.


Step 2:
Create a new ASP.NET Web Application. Drag a TreeView in 'Default.aspx' from the Toolbox.
Step 3:
Add a connection string in the 'Web.config' file:
<connectionStrings>
<add name="ConString" connectionString="Data source=yourservername; User Id=youruserid; Password=yourpassword; Initial Catalog=yourdatabasename;"/>
</connectionStrings>
Step 4:
Write the following code in the code behind file, 'Default.aspx.cs':
using System.Data.SqlClient;
SqlConnection con;
SqlCommand cmd;
SqlDataAdapter sda;
DataTable dt, dt2, dt3;
TreeNode CountryNode, StateNode, CityNode;
string ConString, CmdString, CountryCode, StateCode, CityCode, CountryName, StateName, CityName;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ConString = WebConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
LoadData();
}
}
private void LoadData()
{
con = new SqlConnection(ConString);
CmdString = "SELECT CountryCode, CountryName FROM Country";
cmd = new SqlCommand(CmdString, con);
sda = new SqlDataAdapter(cmd);
dt = new DataTable();
// CountryName and CountryCode is retrieved in the DataTable 'dt'
sda.Fill(dt);
// Loops through all the Country rows of 'dt'
for (int i = 0; i < dt.Rows.Count; i++)
{
// Current CountryName and CountryCode is stored
CountryCode=dt.Rows[i]["CountryCode"].ToString();
CountryName = dt.Rows[i]["CountryName"].ToString();
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment
It is the same account you read, post and publish with — and you will come straight back to this page.

suray sivaPosted May 26, 2014, 9:17 AM
thanks for given code........HAPPY CODING.......
Goutam SinghPosted Nov 13, 2013, 4:39 AM
Nice Work but the Application is not having .cs File
Bill StrachanPosted Feb 27, 2012, 12:09 PM
In my opinion your approach is not good or scalable and it doesn't "bind" the tables to the treeview, what it does do is iterate over each row of each table and create a node using nested loops. Think of the number of nested queries you're executing against the database to achieve a 3 level hierarchy!! What if you wanted a 4th level or if one of the levels had many rows!! I would suggest a better approach would be to create a stored procedure in SQL Server (assuming that is the data source) and return the resultset as XML. Then you can simply specify the XML as the data source for the treeview. One trip to the database and the rendering would be much faster also. Simples!!
Deepak SharmaPosted Feb 9, 2012, 1:48 AM
Thanks a lot
Maria JohnsonPosted Feb 8, 2012, 11:26 PM
Hi Dhananjay. Its a very useful article for those who wants to learn how to Bind Multiple SQL Server Tables With a TreeView in a Hierarchical Order
Abhi KumarPosted Feb 8, 2012, 11:07 PM
Very useful article. Thanks for sharing.
Nitin SinghPosted Feb 8, 2012, 11:04 PM
That is a great effort and thanks for sharing.
Amit MaheshwariPosted Feb 8, 2012, 6:13 PM
It's a great effort to accomplish such task keep it up and thanks for sharing................