♥ 1.Adım
♥ 2.Adım
♥ 3.Adım
♥ 5.Adım
♥ 6.Adım
♥ 7.Adım
♥ 8.Adım
Select * from TblKategori
Select * from TblKategori where ParentID=2

with cte
as
(
select ID,KategoriAdi,ParentID, 0 AS level , ID as Visited
from TblKategori
where ParentID =2
union all
select TblKategori.ID,TblKategori.KategoriAdi,TblKategori.ParentID, level +1 ,
cte.ParentID
from TblKategori inner join cte
on TblKategori.ParentID=cte.ID
where Visited NOT LIKE ('%<'+convert(varchar(3),TblKategori.ParentID)+'>%')
)
select * from cte D1
<connectionStrings>► 3.Adım Default.aspx sayfasına TreeView kontrolümüzü ekliyoruz
<add name="conn" connectionString="Data Source=192.168.0.3;initial catalog=DBKategori;uid=**;pwd=****;"
providerName="System.Data.SqlClient" />
</connectionStrings>

<asp:TreeView ID="TreeView1" runat="server" ImageSet="Arrows" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged">► 4.Adım Default.cs sayfasına kodlarımızı ekliyoruz
<HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />
<Nodes>
<asp:TreeNode Text="Kategoriler" Value="0"></asp:TreeNode>
</Nodes>
<NodeStyle Font-Names="verdana" Font-Size="11pt" ForeColor="Black" HorizontalPadding="2px"
NodeSpacing="0px" VerticalPadding="0px" />
<ParentNodeStyle Font-Bold="False" />
<SelectedNodeStyle Font-Underline="False" ForeColor="#00cc66" HorizontalPadding="5px" VerticalPadding="0px" />
</asp:TreeView>
namespace SinirsizKategori
{
public partial class Default : System.Web.UI.Page
{
private SqlConnection baglanti;
private SqlDataAdapter komut;
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
baglanti = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
komut = new SqlDataAdapter();
string _query = string.Format("select * from TblKategori");
SqlCommand sorgu = new SqlCommand();
DataSet ds = new DataSet();
try
{
if (baglanti.State == ConnectionState.Closed || baglanti.State == ConnectionState.Broken)
baglanti.Open();
sorgu.Connection = baglanti;
sorgu.CommandText = _query;
sorgu.ExecuteNonQuery();
komut.SelectCommand = sorgu;
komut.Fill(ds);
dt = ds.Tables[0];
}
catch { }
populateData(TreeView1.Nodes[0], getrows("0"));
}
DataRowCollection getrows(string id)
{
DataView dw = new DataView(dt);
dw.RowFilter = "ParentID=" + id;
return dw.ToTable().Rows;
}
void populateData(TreeNode node, DataRowCollection col)
{
foreach (DataRow row in col)
{
TreeNode n = new TreeNode();
n.Text = row[1].ToString();
n.Value = row[0].ToString();
node.ChildNodes.Add(n);
populateData(n, getrows(row[0].ToString()));
}
}
}
}