Get ASP.NET MVC How to Tips & Tricks - click here
Your details are safeBuyer's Satisfaction Contact Us
 Welcome Guest | Register | Login Why online training? | Buyers FAQs | Testimonials | Contact us 


Subscribe to Tutorials feed

Our Quality Professors

Our professors includes
Contact us for training ...

Technology Tags

ADO.NET AdRotator ASP.NET ASP.NET-AJAX Authentication-and-Authorization---LoginView BulletedList Button Caching Calendar Chart CheckBoxList Cookies CSharp CSS Cusotm-Control Debugging-and-Tracing DetailsView DropDownList-and-ListBox DropDownList-and--ListBox Error-handling FileUpload Forms-Authentication GridView HiddenField HTML HyperLink Image ImageButton ImageMap Javascript jQuery jQuery-Ajax jQuery-Attributes jQuery-effects jQuery-Effects jQuery-Events jQuery-How-to-Solution jQuery-Manipulations jQuery-Selectors jQuery-Traversing Label-and-Literal LinkButton ListView Localization-and-Globalization Login LoginName LoginStatus LoginStatus-and-LoginName-and-other-controls LoginView MasterPage Menu Meta-tags Panel PlaceHolder RadioButtonList Repeater Security Session-Management TextBox Themes-and-Skins Url-Routing User-Control Validations visual-studio Webconfig Working-with-Files-and-Folders

Search Tutorials



More ...


How to generate Menu from the database?

by: Itfunda Product Type: Tips and Tricks (Books) Technologies: ASP.NET  Menu 

To automatically generate horizontal or vertical menu from the database, we can follow this approach.

<asp:Menu ID="Menu1" runat="server" Orientation="Horizontal" />

Code Behind
protected void Page_Load(object sender, EventArgs e)
    if (!IsPostBack)
private void GetMenuData()
    DataTable table = new DataTable();
    // get the connection
    using (SqlConnection conn = new SqlConnection(_connStr))
        // write the sql statement to execute
        string sql = "SELECT MenuId, MenuName, MenuDescription, ParentMenuId, MenuUrl FROM Menus";
        // instantiate the command object to fire
        using (SqlCommand cmd = new SqlCommand(sql, conn))
            // get the adapter object and attach the command object to it
            using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
                // fire Fill method to fetch the data and fill into DataTable
    // populate the parent node first
    DataView view = new DataView(table);
    view.RowFilter = "ParentMenuId is NULL";
    foreach (DataRowView row in view)
        MenuItem menuItem = new MenuItem(row["MenuName"].ToString(),
        menuItem.NavigateUrl = row["MenuUrl"].ToString();
        AddChildItems(table, menuItem);
private static 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(),
        childItem.NavigateUrl = childView["MenuUrl"].ToString();
        AddChildItems(table, childItem);
In the above code snippet, we have an asp:Menu control on the .aspx page with Orientation as “Horizontal”.

In the code behind, first we have retrieved the menu items from the database (look at my database table structure above). The record with ParentMenuId as “NULL” will be the parent item of the menu and rest will be the child (It is very important that you organize the records in such a way that it forms a hierarchical menu items). Once we have the records into the DataTable, we can filter it based on the ParentMenuID to “NULL” to get the parent node and add the menu Item to the Menu then we have called AddChildItems method to fetch the child items from the DataTable and added into the Menu (AddChildITems is the recursive function to make sure that it goes deep into several herarchy of the items and sub items).


This post is the part of following product

.NET How to Tips and Tricks (700+)

.NET How to Tips and Tricks (700+) This ".NET How to Tips and Tricks" contains solutions of hundreds of technical problems related with ASP.NET, ASP.NET MVC, SQL Serer, jQuery etc. that help you to develop real time .NET projects easily. It has following features
  • Video tutorials
  • Demo projects source code
  • eBook with to-the point explanations & output
prepared by Sheo Narayan (Ex Microsoft MVP).

 Latest tutorials

 Posts from Itfunda

  • jQuery Essentials Online Training
    This training has been designed for those who has basic knowledge of JavaScript and want to quickly learn jQuery within few days. Along with this course you will also get a comprehensive jQuery e-book covering more than 100 jQuery methods and a sample application with source code covering more than 100 methods.
    | Final Price: Rs 2777.00/$59.00
    More details ...

  • ASP.NET 4 Online Training with C#
    ASP.NET 4.0 using C# - A comprehensive online training on ASP.NET 4.0 using Visual C# 2013. This training gives you real time scenario based knowledge on C#, ASP.NET and ASP.NET AJAX that help you to work on any kind of web development projects in .NET technologies. This training is delivered by Mr. Sheo Narayan (Ex Microsoft MVP). Suitable for Windows developer or a complete freshers in the IT Industry who know HTML, CSS and JavaScript.
    List Price: $350.00 Discount: 25.00% | Final Price: Rs 13125.00/$262.50
    More details ...

Share this to:

Facebook Twitter LinkedIn Google+ Pinterest Email Addthis

About Us | Contact Us | Partners | Privacy Policy | Terms and Conditions | Buyer's FAQs | Seller's FAQs | Currency Converter | Go Top

Notice: If you found someone plagiarising our content/materials, kinldy let us know.

© SN ITFunda Services LLP. Site design and layout is copyright to SN ITFunda Services LLP.
Copying or duplicating or mimicking the site design and layout is prohibited and will be treated as a legal offence and will be prosecuted.
Logos, company, product, service or other names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 11032014 | 10/25/2016 6:46:06 PM