2.(cs)
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.Data.SqlClient;
using System.Configuration;
public partial class CS : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString.ToString());
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = this.GetData("SELECT cid,cname, pid , pname FROM TopParent where pid=10");
this.PopulateTreeView(dt, 0, null);
BindRepeaterRight();
}
}
private void PopulateTreeView(DataTable dtParent, int parentId, TreeNode treeNode)
{
foreach (DataRow row in dtParent.Rows)
{
TreeNode child = new TreeNode
{
Text = row["cName"].ToString(),
Value = row["cId"].ToString()
};
if (parentId == 0)
{
TreeView1.Nodes.Add(child);
DataTable dtChild = this.GetData("select * from TopParent WHERE pid = " + child.Value);
// PopulateTreeView(dtChild, int.Parse(child.Value), child);
Page.ClientScript.RegisterStartupScript(this.GetType(), "ch", "<script>alert('Parent Node Created -" + child.Text + "..')</script>");
if (dtChild.Rows.Count > 0)
{
//........................
int ParentCid = Convert.ToInt32(dtChild.Rows[0]["CID"].ToString());
string query = "select * from TopParent WHERE cid = " + ParentCid;
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
SqlDataReader dr;
dr = cmd.ExecuteReader();
if (dr.Read())
{
String Cname = dr["cname"].ToString();
String cid = dr["cid"].ToString();
DataTable dtnChild = this.GetData("select cid,cname from TopParent WHERE pid = " + cid);
TreeNode nnode = new TreeNode
{
Text = row["cname"].ToString(),
Value = row["cid"].ToString()
};
TreeView1.Nodes.Add(nnode);
PopulateTreeView(dtnChild, int.Parse(cid), nnode);
Page.ClientScript.RegisterStartupScript(this.GetType(), "ch", "<script>alert('Child Node Created -" + Cname + "..')</script>");
}
con.Close();
}
PopulateTreeView(dtChild, int.Parse(child.Value), child);
}
else
{
treeNode.ChildNodes.Add(child);
}
}
}
private DataTable GetData(string query)
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
}
}
return dt;
}
}
}
(3) . Database
USE [TreeView]
GO
/****** Object: Table [dbo].[TopParent] Script Date: 11/09/2017 17:48:18 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[TopParent](
[Id] [int] IDENTITY(1,1) NOT NULL,
[PId] [bigint] NULL,
[PName] [nvarchar](20) NULL,
[Cid] [bigint] NULL,
[CName] [nvarchar](max) NULL
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[TopParent] ON
INSERT [dbo].[TopParent] ([PId], [PName], [Cid], [CName]) VALUES ( NULL, NULL, 10, N'India')
INSERT [dbo].[TopParent] ( [PId], [PName], [Cid], [CName]) VALUES ( 10, N'India', 1010, N'Delhi')
INSERT [dbo].[TopParent] ([Id], [PId], [PName], [Cid], [CName]) VALUES (3, 10, N'India', 1011, N'UP')
INSERT [dbo].[TopParent] ([Id], [PId], [PName], [Cid], [CName]) VALUES (4, 10, N'India', 1012, N'MP')
INSERT [dbo].[TopParent] ([Id], [PId], [PName], [Cid], [CName]) VALUES (5, 10, N'India', 1013, N'Haryana')
INSERT [dbo].[TopParent] ([Id], [PId], [PName], [Cid], [CName]) VALUES (6, 1010, N'Delhi', 101010, N'Laxmi Nagar')
INSERT [dbo].[TopParent] ([Id], [PId], [PName], [Cid], [CName]) VALUES (9, 1011, N'UP', 101110, N'Vaishali')
INSERT [dbo].[TopParent] ([Id], [PId], [PName], [Cid], [CName]) VALUES (11, 1012, N'MP', 101210, N'Satna')
INSERT [dbo].[TopParent] ([Id], [PId], [PName], [Cid], [CName]) VALUES (12, 1013, N'Haryana', 101310, N'Rewari')
INSERT [dbo].[TopParent] ([Id], [PId], [PName], [Cid], [CName]) VALUES (7, 1010, N'Delhi', 101011, N'Preet Vihar')
INSERT [dbo].[TopParent] ([Id], [PId], [PName], [Cid], [CName]) VALUES (8, 1010, N'Delhi', 101012, N'Anand Vihar')
INSERT [dbo].[TopParent] ([Id], [PId], [PName], [Cid], [CName]) VALUES (10, 1011, N'UP', 101111, N'Ghaziabad')
SET IDENTITY_INSERT [dbo].[TopParent] OFF
Pleae Solve this .