Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
hello
how can create dynamic
    from database using vb.net or c# in asp.net
    Notes ......
    this list Non-specific levels
    EX....

    • Level 1

      • Levele 2

        • Levele 3
          ...................... And so on


Posted

Please refer the links Link1
Link2

Understand and manage your code accordingly..
 
Share this answer
 
I've got a solution now
C# Code ---------------------
C#
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) {
			//Dim handler As String = dr("Handler").ToString()
			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
------------------------------------------------------

VB
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 handler As String = dr("Handler").ToString()
                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 ======
=======================
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900