title: How to Create Dynamic Menu from Database using Menu control in asp.net?
body: <p>I want to create a menu from Database and show in Menu Control.</p>
  
  <p>Code Here in .aspx page:</p>
  
  <pre><code> &lt;asp:Menu ID="Menu1" Orientation="horizontal" StaticMenuItemStyle-CssClass="menuItem"
                              DynamicMenuItemStyle-CssClass="menuItem" runat="server"&gt;
  </code></pre>
  
  <p>In .cs Page of Master:</p>
  
  <pre><code> protected void Page_Load(object sender, EventArgs e)
      {
          if (!IsPostBack)
          {
  
              populateMenuItem();
          }
  
      }
  
      private void populateMenuItem()
      {
  
          DataTable menuData = GetMenuData();
          AddTopMenuItems(menuData);
  
      }
      /// Filter the data to get only the rows that have a
      /// null ParentID (This will come on the top-level menu items)
  
      private void AddTopMenuItems(DataTable menuData)
      {
          DataView view = new DataView(menuData);
          view.RowFilter = "DepartmentParentID IS NULL";
          foreach (DataRowView row in view)
          {
              //MenuItem newMenuItem = new MenuItem(row["DepartmentName"].ToString(), row["DepartmentID"].ToString());
              MenuItem newMenuItem = new MenuItem(row["DepartmentName"].ToString(), row["DepartmentID"].ToString());
  
              Menu1.Items.Add(newMenuItem);
              AddChildMenuItems(menuData, newMenuItem);
          }
  
      }
      //This code is used to recursively add child menu items by filtering by ParentID
  
      private void AddChildMenuItems(DataTable menuData, MenuItem parentMenuItem)
      {
          DataView view = new DataView(menuData);
          view.RowFilter = "DepartmentParentID=" + parentMenuItem.Value;
          foreach (DataRowView row in view)
          {
              MenuItem newMenuItem = new MenuItem(row["DepartmentName"].ToString(), row["DepartmentID"].ToString());
              parentMenuItem.ChildItems.Add(newMenuItem);
              AddChildMenuItems(menuData, newMenuItem);
          }
      }
  
  
      private DataTable GetMenuData()
      {
          using (SqlConnection con = new SqlConnection(conStr))
          {
  
              using (SqlCommand cmd = new SqlCommand("SELECT  DepartmentID,OfficeID,DepartmentName,DepartmentParentID,IsActive,CreatedByID,CreatedDate,LastModifiedByID,LastModifiedDt FROM DepartmentMst", con))
              {
                  SqlDataAdapter da = new SqlDataAdapter(cmd);
                  DataTable dt = new DataTable();
                  da.Fill(dt);
                  return dt;
              }
  
          }
      }
  </code></pre>
  
  <p>The Problem is in AddTopMenuItems() Method where it shows "Object reference not set to instance of an object" at line Menu1.Items.Add(newMenuItem);
  Don't know Why?</p>
  
  <p>Here is Data in SQLSERVER2008 DepartmentMst:</p>
  
  <pre><code>DepartmentID  DepartmentName IsActive   DepartmentParentID
  1               HR            1            NULL
  2               IT            1            NULL
  3            Operations    1                NULL
  4            Desktop Engineer 1             2
  5           Network Engineer  1             2
  6           Employee Salary   1             1
  </code></pre>
  
  <p>When the DepartmentParentID is NULL then it is Main Menu and if not null then it is Child node with respected to its Parent ID.</p>
  
  <p>Sample here <a href="http://chandradev819.wordpress.com/2011/07/03/how-to-bind-asp-net-menu-control-with-database/" rel="nofollow">http://chandradev819.wordpress.com/2011/07/03/how-to-bind-asp-net-menu-control-with-database/</a></p>
  
  <p>Help Appreciated!</p>