Hello Guys !!
I have already asked the same question but now i am again asking with new requirment, so please read carefully and solve my issues.
My Requirment:
- How to create n level of Treeview in asp.net c# from SQL Database Record.
Suppose if CID is also in PID column, Node must be created on that CID and also must be expand button to show the child of that cID.
What i have done in coding let's see...
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="N-level-Treeview.aspx.cs"
Inherits="N_level_Treeview" %>
NodeSpacing="0px" VerticalPadding="2px">
VerticalPadding="0px" />
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 N_level_Treeview : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = this.GetData("SELECT cId, cName FROM TopBill");
this.PopulateTreeView(dt, 0, null);
}
}
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 srNo,cid, cName FROM TopBill WHERE pid = " + child.Value);
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;
}
}
}
-------------- Current Output of my Code is ....

Required Result format is like---------
In above picture , I am trying to show that if India has some child node, India Must me expandable so that i can see delhi, If again Delhi Has some child Like Preet vihar , Laxmi Nagar, Delhi also must be expandable to see their child and so on....
Please solve this issues..


Nitin SontakkeRank #264 · 9.6K reputationPosted Nov 20, 2017, 12:11 AM EST
Nitin SontakkeRank #264 · 9.6K reputationPosted Nov 20, 2017, 2:26 AM EST
Vivek Kumar Vishwas180 reputationPosted Nov 20, 2017, 12:50 AM EST
Vivek Kumar Vishwas180 reputationPosted Nov 20, 2017, 6:57 PM EST
Vivek Kumar Vishwas180 reputationPosted Nov 20, 2017, 10:10 PM EST
Nitin SontakkeRank #264 · 9.6K reputationPosted Nov 21, 2017, 12:01 AM EST