title: create dynamically menu and submenu from database in asp .net
body: <p>I want to display menu dynamically from database. But My menu items are displaying but when i used to click on menu,submenu is not displaying.please tell me where is the error</p>
  
  <p>My code is</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
      if(!IsPostBack)
      { GetMenuData(); }
  }
  private void GetMenuData()
  {
      DataTable table = new DataTable();
      SqlConnection conn = new SqlConnection(@"Data Source=ST015\SQLEXPRESS;Initial Catalog=MyData;Integrated Security=True");
      string sql = "select menuId,Menuname,MenuDescription,ParentMenuid,MenuUrl from Categories";
      SqlCommand cmd = new SqlCommand(sql, conn);
      SqlDataAdapter da = new SqlDataAdapter(cmd);
      da.Fill(table);
      DataView view = new DataView(table);
      view.RowFilter = "ParentMenuId is NULL";
      foreach (DataRowView row in view)
      {
          MenuItem menuItem = new MenuItem(row["MenuName"].ToString(),
              row["MenuId"].ToString());
          menuItem.NavigateUrl = row["MenuUrl"].ToString();
          Menu1.Items.Add(menuItem);
          AddChildItems(table, menuItem);
      }
  }
  private void AddChildItems(DataTable table, MenuItem menuItem)
  {
      DataView viewItem = new DataView(table);
      viewItem.RowFilter = "ParentMenuId=" + menuItem.Value;
      foreach (DataRowView childView in viewItem)
      {
          MenuItem childItem = new MenuItem(childView["MenuName"].ToString(),
              childView["MenuId"].ToString());
          childItem.NavigateUrl = childView["MenuUrl"].ToString();
          menuItem.ChildItems.Add(childItem);
          AddChildItems(table, childItem);
      }
  }
  </code></pre>