title: Generate Nested Menu from datatable using c# as ul list not Asp.net Menu control
body: <p>I need to generate a custom menu which has submenu's using list ul from the <code>DataTable</code> </p>
  
  <p>Below is the sample of Database and sample HTML ul list with dummy data.</p>
  
  <pre><code>PID         MENU                 Handler                  PageLangID  ParentID    IssueID     CatID       MenuPosition
  ----------- -------------------- ------------------------ ----------- ----------- ----------- ----------- ------------
  
  6           Business             Category.aspx    1           6           1           16          1
  6           Culture              Category.aspx    1           6           1           3           1
  6           Economy              Category.aspx    1           6           1           2           1
  6           Finance              Category.aspx    1           6           1           19          1
  6           Infrastructure       Category.aspx    1           6           1           17          1
  6           Lifestyle            Category.aspx    1           6           1           20          1
  6           Others               Category.aspx    1           6           1           21          1
  6           People               Category.aspx    1           6           1           7           1
  6           Politics             Category.aspx    1           6           1           1           1
  6           Sports               Category.aspx    1           6           1           4           1
  12          1002                  Default.aspx             1           12          3           1           1
  12          1003                  Default.aspx             1           12          4           1           1
  12          1006                  Default.aspx             1           12          1           1           1
  12          1009                  Default.aspx             1           12          5           1           1
  1           Home                 Default.aspx             1           0           1           1           10
  11          Video                Videos.aspx              1           10          1           1           10
  2           About Us             Page.aspx                1           0           1           1           20
  5           Articles             Articles.aspx            1           0           1           1           20
  6           Categories           Category.aspx    1           0           
  </code></pre>
  
  <p>DESIRED HTML OUTPUT</p>
  
  <pre><code>&lt;div id="nav-wrapper"&gt;
  &lt;ul id="nav" class="dropdown dropdown-linear"  &gt;
      &lt;li&gt;&lt;span class="dir"&gt;&lt;a href="./"&gt;Home&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;
      &lt;li &gt;&lt;span class="dir"&gt;&lt;a href="ultimate.linear.html"&gt;About Us&lt;/a&gt;&lt;/span&gt;
          &lt;ul &gt;
              &lt;li&gt;&lt;a href="./"&gt;History&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="./"&gt;Our Vision&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="./"&gt;The Team&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="./"&gt;Clients&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="./"&gt;Testimonials&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="./"&gt;Press&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="./"&gt;FAQs&lt;/a&gt;&lt;/li&gt;
          &lt;/ul&gt;
      &lt;/li&gt;
  
      &lt;li class="active" &gt;&lt;span class="dir"&gt;&lt;a href="ultimate.linear-active.html"&gt;Categories&lt;/a&gt;&lt;/span&gt;
          &lt;ul&gt;
              &lt;li&gt;&lt;a href="./"&gt;Politics&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="./"&gt;Economy&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="./"&gt;Finance&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="./"&gt;Business&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="./"&gt;Group News&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="./"&gt;Culture&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="./"&gt;Lifestyle&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="./"&gt;Sports&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="./"&gt;Infrastructure&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="./"&gt;Book Review&lt;/a&gt;&lt;/li&gt;   
              &lt;li&gt;&lt;a href="./"&gt;Others&lt;/a&gt;&lt;/li&gt;                
          &lt;/ul&gt;
      &lt;/li&gt;
  &lt;/ul&gt;
  &lt;/div&gt; 
  </code></pre>
  
  <p>I don't want to use nested repeater controls. I would appreciate sample code which i can work with.</p>
  
  <p><strong>UPDATED</strong>: This code doesn't work, definitely i am doing something wrong</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
  
      DataSet ds = new DataSet();
      ds = DataProvider.Connect_Select(strSql);
       DataTable dt = ds.Tables[0];
  
      //dt.Select("ParentID == 0") ;
      var s = GenerateUL(dt.Select("ParentID == 0"));
      Response.Write(s);
  
  }
  
  
  private string GenerateUL(var menus)
  {
      var sb = new StringBuilder();
  
      sb.AppendLine("&lt;ul&gt;");
      foreach (var menu in menus)
      {
          if (menu.Menus.Any())
          {
              sb.AppendLine("&lt;li&gt;" + menu.Text);
              sb.Append(GenerateUL(menu.Menus.AsQueryable()));
              sb.AppendLine("&lt;/li&gt;");
          }
          else
              sb.AppendLine("&lt;li&gt;" + menu.Text + "&lt;/li&gt;");
      }
      sb.AppendLine("&lt;/ul&gt;");
  
      return sb.ToString();
  }
  </code></pre>
  
  <p><strong>LATEST UPDATE BASED ON DANI SOLUTION</strong></p>
  
  <p>It seems to work fine with his data sample but when i use it with my actual data it generate StackOverflowException was unhandled.</p>
  
  <p>Below is the screen shot of the error.
  <img src="https://i.stack.imgur.com/l67bd.jpg" alt="enter image description here"></p>
  
  <p>Error is generated when it goes into sort of infinate loop where <code>PID=6</code> My actual data which is show above has 23 records &amp; is result of UNION from different tabel that is the reason i have multiple rows in table where <code>PID=6</code> i am afraid it will also do same where <code>pid=12</code>.</p>
  
  <p>Even if i catch the exception my website still crash due to this ...</p>
  
  <p><strong>Latest CODE</strong></p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
      string strSql = "SELECT DISTINCT PID, MENU, Handler,PageLangID, ParentID,IssueID, CatID,MenuPosition FROM MENUTABLE ";
  
      DataSet ds = new DataSet();
      ds = DataProvider.Connect_Select(strSql);
      DataTable table = ds.Tables[0];
      DataRow[] parentMenus = table.Select("ParentId = 0");
           var sb = new StringBuilder();
           string unorderedList = GenerateUL(parentMenus, table, sb);
      }
  
      private string GenerateUL(DataRow[] menu, DataTable table, StringBuilder sb)
      {
      sb.AppendLine("&lt;ul&gt;");
  
      try
      {
  
          if (menu.Length &gt; 0)
          {
              foreach (DataRow dr in menu)
              {
                  ctr = ctr + 1;
                  string handler = dr["Handler"].ToString();
                  string menuText = dr["MENU"].ToString();
                  string line = String.Format(@"&lt;li&gt;&lt;a href=""{0}""&gt;{1}&lt;/a&gt;", handler, menuText);
                  sb.Append(line);
  
                  string pid = dr["PID"].ToString();
  
                  DataRow[] subMenu = table.Select(String.Format("ParentId = {0}", pid));
                  if (subMenu.Length &gt; 0)
                  {
                      var subMenuBuilder = new StringBuilder();
                      sb.Append(GenerateUL(subMenu, table, subMenuBuilder));
                  }
                  sb.Append("&lt;/li&gt;");
              }
          }
      }
      catch (Exception ex)
      {
      }
  
      sb.Append("&lt;/ul&gt;");
      return sb.ToString();
  }
  </code></pre>
  
  <p>Update: When i change my query which get me result below then it works fine, but it would be good to make it work on actual data which is shown first in the question.</p>
  
  <pre><code>PID         MENU                 Handler                  PageLangID  ParentID    IssueID     CatID       MenuPosition
  ----------- -------------------- ------------------------ ----------- ----------- ----------- ----------- ------------
  1           Home                 Default.aspx             1           0           1           1           10
  2           About Us             Page.aspx                1           0           1           1           20
  3           News                 News.aspx                1           0           1           1           30
  5           Articles             Articles.aspx            1           0           1           1           20
  6           Categories           Category.aspx    1           0           1           1           25
  </code></pre>