Click here to Skip to main content
15,881,092 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

how to change the menus bar from the database using mysql and .net based on the user role.
its basically an authorization based on the user role.
please reply me asap.

What I have tried:

I have tried with

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


public partial class Home : System.Web.UI.Page
{
    
    #region Page Load
    /// <summary>
    /// Loads the intial initial contents required for the page
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public void Page_Load(object sender, EventArgs e)
    {
       
        
        if (!IsPostBack)
        {
            DataTable menuData = null;
            try
            {
                menuData = new DataTable();
                menuData = GetMenuData();
                AddTopMenuItems(menuData);
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
            finally
            {
                menuData = null;
            }
        }
    }
    #endregion

    #region Function for getting data for menu
    /// <summary>
    /// Get's the data from database for menu
    /// </summary>
    /// <returns>Datatable of menu items</returns>
    public DataTable GetMenuData()
    {
        try
        {
            
            using (MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString))
            {
                MySqlCommand cmd = new MySqlCommand("spmenuitem", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@userid", "K001");
                DataTable dtMenuItems = new DataTable();
                MySqlDataAdapter sda = new MySqlDataAdapter(cmd);
                sda.Fill(dtMenuItems);
                cmd.Dispose();
                sda.Dispose();
                return dtMenuItems;
            }
        }
        catch (Exception ex) {
            Response.Write(ex.Message);
        }
        return null;
    }
    #endregion

    #region Function for adding top menu items
    /// <summary>
    /// Adds the top/parent menu items for the menu
    /// </summary>
    /// <param name="menuData"></param>
    public void AddTopMenuItems(DataTable menuData)
    {
       
        DataView view = null;
        try
        {
            view = new DataView(menuData);
            var f = menuData.TableName;
            //view.RowFilter = "ParentID IS NULL";
            foreach (DataRowView row in view)
            {
               
                //Adding the menu item
                MenuItem newMenuItem = new MenuItem(row["parantid"].ToString(), row["menuid"].ToString()); //MenuItem("Home","1"); 
             // menuBar.Items.Add(newMenuItem);
                AddChildMenuItems(menuData, newMenuItem);
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        finally {
            view = null;
        }
    }
    #endregion

    #region Function for adding child menu items from database
    /// <summary>
    /// This code is used to recursively add child menu items by filtering by ParentID
    /// </summary>
    /// <param name="menuData"></param>
    /// <param name="parentMenuItem"></param>
    public void AddChildMenuItems(DataTable menuData, MenuItem parentMenuItem)
    {
        DataView view = null;
        try
        {
            view = new DataView(menuData);
            view.RowFilter = "parantid=" + parentMenuItem.Value;
            foreach (DataRowView row in view)
            {
                MenuItem newMenuItem = new MenuItem(row["parantid"].ToString(), row["menuid"].ToString());
                newMenuItem.NavigateUrl = row["navigateurl"].ToString();
                parentMenuItem.ChildItems.Add(newMenuItem);
                AddChildMenuItems(menuData, newMenuItem);
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        finally
        {
            view = null;
        }
    }
    #endregion
}

CREATE DEFINER=`root`@`localhost` PROCEDURE `spmenuitem`(in userid varchar(50))
BEGIN
declare val varchar(1000); declare pos int;declare len int;declare value char(2);
drop table if exists temp ;
create table temp(menuid int, text varchar(50), description varchar(50), parantid int, navigateurl varchar(100));
set val=(select sum(privilageid) from grpprivmst where grpcode in (SELECT grpcode FROM usergrpmap WHERE userid=userid));
set pos=1;
set len =Length(val);
while(len!=0) do
set value = substring(val,pos,1);
if value=1 then
select value;
insert into temp select * from menumst where menuid=pos;
end if;
set pos=pos+1;
set len=len-1;
end while ;
insert into temp values(1,'admin','administrator',2,'Home.aspx');
select * from temp order by menuid asc;
END


but its not working...
Posted
Updated 9-Jun-16 21:12pm
v2
Comments
SRS(The Coder) 10-Jun-16 3:19am    
Basically what you can do is pass the role id and fetch the menu details from the database as per the roleid only.

RoleID should be maintained in DB with any relation to the menu data table. While fetching we will pass the roleid from frontend and fetch the menu data for the role only.
George Jonsson 11-Jun-16 5:16am    
Is this spelled correctly?
row["parantid"].ToString()
or should it be
row["parentid"].ToString()

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900