Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Dynamically created menu bar from the database according to user credential using C#.NET

1.00/5 (1 vote)
11 Jun 2013CPOL 17.4K  
How to populate a dynamically created menu bar from the database according to user credentials using C# .NET and ASP.NET.

Introduction

This articles describe how to populate a dynamically created menu bar from the database according to user credentials. I used .NET Framework 4.0 and SQL server 2008 R2 as a database and ADO.NET.

Using the code

The Default.aspx.cs page contains the code for the menu bar:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace MenuApplication
{
    public partial class _Default : System.Web.UI.Page
    {
        const string connectionString = "Data Source=NEXDESKKOL001;Initial Catalog;";
        string DBConnectionString = ConfigurationManager.ConnectionStrings[
          "TConnectionString"].ConnectionString;
        SqlConnection SQLCon = new SqlConnection(
          ConfigurationManager.ConnectionStrings[
          "TConnectionString"].ConnectionString.ToString());
        SqlCommand SQLCmd = new SqlCommand();
        DataSet dst = new DataSet();
        SqlDataAdapter dadMenus = new SqlDataAdapter();
        SqlDataAdapter dadSubMenues = new SqlDataAdapter();
        SqlDataAdapter dadSubSubMenues = new SqlDataAdapter();

        Menu myownMenu = new Menu();
        string query = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            SQLCon = new SqlConnection(DBConnectionString);
            if (!IsPostBack)
            {

                DataTable menuData = null;
                try
                {
                    menuData = new DataTable();
                    menuData = GetMenuData();
                    AddTopMenuItems(menuData);
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }
                finally
                {
                    menuData = null;
                }

            }
        }

        private DataTable GetMenuData()
        {
            try
            {
                using (SqlConnection con = new SqlConnection(
                  ConfigurationManager.ConnectionStrings[
                  "TConnectionString"].ConnectionString))
                {
                    SqlCommand cmd = new SqlCommand("SP_UserLogin", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@Userid", 
                      SqlDbType.NVarChar).Value =Convert.ToString(Session["UserId"]);
                    DataTable dtMenuItems = new DataTable();
                    SqlDataAdapter sda = new SqlDataAdapter(cmd);
                    sda.Fill(dtMenuItems);
                    cmd.Dispose();
                    sda.Dispose();
                    return dtMenuItems;
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
            return null;
        }


        private void AddTopMenuItems(DataTable menuData)
        {
            DataView view = null;
            try
            {
                view = new DataView(menuData);
                view.RowFilter = "ParentID =0";
                foreach (DataRowView row in view)
                {
                    //Adding the menu item
                    MenuItem newMenuItem = new MenuItem(Convert.ToString(row["MenuName"]), 
                      Convert.ToString(row["MenuID"]), 
                      Convert.ToString(row["MenuImageUrl"]), 
                      Convert.ToString(row["MenuNavigationURL"]));
                    newMenuItem.NavigateUrl = Convert.ToString(row["MenuNavigationUrl"]);
                    newMenuItem.ImageUrl = Convert.ToString(row["MenuImageUrl"]);
                    menuBar.Items.Add(newMenuItem);
                    AddChildMenuItems(menuData, newMenuItem);
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
            finally
            {
                view = null;
            }
        }

        private void AddChildMenuItems(DataTable menuData, MenuItem parentMenuItem)
        {
            DataView view = null;
            try
            {
                view = new DataView(menuData);
                view.RowFilter = "ParentID=" + parentMenuItem.Value;
                foreach (DataRowView row in view)
                {
                    MenuItem newMenuItem = new MenuItem(Convert.ToString(row["MenuName"]), 
                      Convert.ToString(row["MenuID"]), 
                      Convert.ToString(row["MenuImageUrl"]), 
                      Convert.ToString(row["MenuNavigationURL"]));
                    newMenuItem.NavigateUrl = row["MenuNavigationUrl"].ToString();
                    newMenuItem.ImageUrl = Convert.ToString(row["MenuImageUrl"]);
                    parentMenuItem.ChildItems.Add(newMenuItem);
                    AddChildMenuItems(menuData, newMenuItem);
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
            finally
            {
                view = null;
            }
        }
    }
}

License

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