Create TreeView in ASP.Net With SQL Database Tables

A TreeView control is a useful control in ASP.NET.

Introduction

Create TreeView in ASP.NET C# with SQL Database.

Step 1: Database

First create a table in the database as in the following.

Table 1: State

  1. create table IN_State  
  2. (  
  3.    S_Id int identity(1,1) primary key,  
  4.    S_Name varchar(30)  
  5. )  
Table 2: City
  1. create table IN_City  
  2. (  
  3.    C_Id int identity(1,1) primary key,  
  4.    C_Name varchar(30),  
  5.    S_Id int foreign key references IN_State(S_Id)  
  6. )  
Step 2: Visual Studio
  1. Go to the project the Solution Explorer.
  2. Add a new item as in the following:

    Add new Item
    Figure 1: Add new Item

  3. Add a Web Form.

    Add Web From
    Figure 2: Add Web From

Step 3: From UI Side

Now add a TreeView control on the .aspx page as in the following UI code:

  1. <%@ Page Title="" Language="C#" MasterPageFile="~/Master/Master.master" AutoEventWireup="true" CodeFile="TreeView.aspx.cs" Inherits="UI_TreeView" %>  
  2.   
  3. <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">  
  4. </asp:Content>  
  5. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">  
  6.   
  7.     <asp:TreeView runat="server" ID="tview">  
  8.     </asp:TreeView>  
  9.   
  10. </asp:Content>  
Step 4: From UI Code Side

Now right-click on the page design and go to View Code and click it.

Viewcode
Figure 3: Viewcode

Code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Configuration;  
  8. using System.Data;  
  9. using System.Data.SqlClient;  
  10.   
  11.   
  12. public partial class UI_TreeView : System.Web.UI.Page  
  13. {  
  14.     protected void Page_Load(object sender, EventArgs e)  
  15.     {  
  16.         if (!Page.IsPostBack)  
  17.         {  
  18.             string connection = ConfigurationManager.ConnectionStrings["connstring"].ConnectionString;  
  19.             using (SqlConnection Conn = new SqlConnection(connection))  
  20.             {  
  21.                 string State = "Select * from IN_State";  
  22.                 string City = "Select * from IN_City";  
  23.   
  24.                 string Treeview = State + ";" + City;  
  25.   
  26.                 DataSet ds = new DataSet();  
  27.                 SqlDataAdapter da = new SqlDataAdapter(Treeview, Conn);  
  28.                 da.Fill(ds);  
  29.                 ds.Tables[0].TableName = "IN_State";  
  30.                 ds.Tables[1].TableName = "IN_City";  
  31.   
  32.                 DataRelation dr = new DataRelation("StateCity", ds.Tables["IN_State"].Columns["S_Id"], ds.Tables["IN_City"].Columns["S_Id"]);  
  33.                 ds.Relations.Add(dr);  
  34.   
  35.                 foreach (DataRow drState in ds.Tables["IN_State"].Rows)  
  36.                 {  
  37.                     TreeNode NDState = new TreeNode();  
  38.                     NDState.Text = drState["S_Name"].ToString();  
  39.                     NDState.Value = drState["S_Id"].ToString();  
  40.                     tview.Nodes.Add(NDState);  
  41.   
  42.                     foreach (DataRow drCity in drState.GetChildRows("StateCity"))  
  43.                     {  
  44.                         TreeNode NDCity = new TreeNode();  
  45.                         NDCity.Text = drCity["C_Name"].ToString();  
  46.                         NDCity.Value = drCity["C_Id"].ToString();  
  47.                         NDState.ChildNodes.Add(NDCity);  
  48.                     }  
  49.                 }  
  50.             }  
  51.         }  
  52.     }  
  53. }  
Note: Please maintain a database connection string in your project.

Now the TreeView control code is complete. Run the design page and display the created TreeView as in the following.

Treeview
Figure 4: Treeview

I hope you have understood how to create a TreeView with a SQL table.


Similar Articles