I've got a solution now
C# Code ---------------------
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet();
SqlConnection cn = new SqlConnection("");
SqlCommand cmd = new SqlCommand("SELECT * FROM Tree", cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
DataTable table = ds.Tables(0);
DataRow[] parentMenus = table.Select("ParentId = 0");
dynamic sb = new StringBuilder();
string unorderedList = GenerateUL(parentMenus, table, sb);
Response.Write(unorderedList);
}
private string GenerateUL(DataRow[] menu, DataTable table, StringBuilder sb)
{
sb.AppendLine("<ul>");
if (menu.Length > 0) {
foreach (DataRow dr in menu) {
string menuText = dr("NameA").ToString();
string line = String.Format("<li><a href="#">{0}</a>", menuText);
sb.Append(line);
string pid = dr("Code").ToString();
DataRow[] subMenu = table.Select(String.Format("ParentId = {0}", pid));
if (subMenu.Length > 0) {
dynamic subMenuBuilder = new StringBuilder();
sb.Append(GenerateUL(subMenu, table, subMenuBuilder));
}
sb.Append("</li>");
}
}
sb.Append("</ul>");
return sb.ToString();
}
======================================================
======================================================
VB.Net Code
------------------------------------------------------
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim ds As New DataSet()
Dim cn As New SqlConnection("")
Dim cmd As New SqlCommand("SELECT * FROM Tree", cn)
Dim da As New SqlDataAdapter(cmd)
da.Fill(ds)
Dim table As DataTable = ds.Tables(0)
Dim parentMenus As DataRow() = table.[Select]("ParentId = 0")
Dim sb = New StringBuilder()
Dim unorderedList As String = GenerateUL(parentMenus, table, sb)
Response.Write(unorderedList)
End Sub
Private Function GenerateUL(ByVal menu As DataRow(), ByVal table As DataTable, ByVal sb As StringBuilder) As String
sb.AppendLine("<ul>")
If menu.Length > 0 Then
For Each dr As DataRow In menu
Dim menuText As String = dr("NameA").ToString()
Dim line As String = [String].Format("<li><a href="#">{0}</a>", menuText)
sb.Append(line)
Dim pid As String = dr("Code").ToString()
Dim subMenu As DataRow() = table.[Select]([String].Format("ParentId = {0}", pid))
If subMenu.Length > 0 Then
Dim subMenuBuilder = New StringBuilder()
sb.Append(GenerateUL(subMenu, table, subMenuBuilder))
End If
sb.Append("</li>")
Next
End If
sb.Append("</ul>")
Return sb.ToString()
End Function
=======================
==== Karam Omran ======
=======================