Click here to Skip to main content
15,886,873 members
Articles / Web Development / ASP.NET
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
1.00/5 (1 vote)
11 Jun 2013CPOL 17.1K   8   2
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)


Written By
Software Developer Nexval Infotech Pvt. Ltd.
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.
This is a Organisation

1 members

Comments and Discussions

 
GeneralMy vote of 1 Pin
Abhishek Pant25-Nov-13 6:21
professionalAbhishek Pant25-Nov-13 6:21 
QuestionDatabase Pin
nnergi26-Jun-13 6:14
nnergi26-Jun-13 6:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.