Role based Menu access using C# in Windows Application

Last Reply 18 days ago By arunkurmi

Posted 22 days ago

After login User1, the main menu is open in main form. Now there are following 2 conditions:

1-User1 will have access to only those Forms where he is granted access by tblRights.According to database (tblRights), User1 has only access to Student Inquiry menu item under Student menu. So the rest of the menu item must be invisible to User1. How to get this solution?

2- After access to only Student Inquiry form, user1 will have only access to View and Save button. Rest of the button must be invisible to User1 untill he is granted access. How to get this solution? ???

tblForms

FormID

FormName

FormCaption

Module

1

frmStuInquiry

Student Inquiry

Student

2

frmStuRegister

Student Registeration

Student

3

frmStuAttendance

Student Attendance

Student

4

frmStuList

Stdent List

Student

5

frmStuPromotion

Student Promotion

Student

tblFormsControl

fCtrlID

fCtrlName

FormID

IsActive

1

View

1

1

2

Save

1

1

3

Update

1

1

4

Delete

1

1

5

Export

1

1

tblRights

RightID

GroupID

UserID

FormID

fCtrlID

IsRights

1

2

2

1

1

1

2

2

2

1

2

1

3

2

2

1

3

0

4

2

2

1

4

0

5

2

2

1

5

0

tblGroup

GroupID

G_Name

1

Admin

2

User1

tblUsers

UserID

UserName

GroupID

1

Admin

1

2

User1

2

Main Menu in the Main Form in the Windows Form

Student

Employee

Fees

Examination

Configuration

Student Inquiry

 

Student Registeration

 

Student Attendance

 

Stdent List

 

Student Promotion

 

 

Results 1 - 5 of 7 12
Posted 22 days ago

Hi smile,

You have not implemented any code for show hide menu and controls.

I would request to try at your end and then report back if any problems and also paste your code.


Posted 22 days ago

I applied code according to ur instruction but unable to get required output. screenshoot is attached

sadsdsdf

and code is 

 

private void GetControl()
        {

            this.Text = "Student Registeration";
            string form = this.Text;
            string name = null;
            foreach (DataGridViewRow items in dGVR.Rows)
            {
                string module = items.Cells[2].Value.ToString();
                bool status = Convert.ToBoolean(items.Cells[4].Value.ToString());
                name = module.ToString();              
            }
           // MessageBox.Show(name);
            if (form == name)
            {
                this.Show();
                    MessageBox.Show("Welcome");
            }
            else
            {
               this.Hide();
               MessageBox.Show("Welcome Bye");
            }

        }

how to f it?? and y datagridview row is unable to get value???


Posted 22 days ago Modified on 22 days ago

Moreover, datagridview row value is not getting the value. screenshoot is attached i saw this by using breakpoint. Moreover, I call this function in the form load event but unable to get solution. form is still being shown. how to fix it???


Posted 22 days ago

Finally,I have solved my problem. but there is only one issue which i want to finalize with u.

I have defined toolStrip and used toolStrip button and toolStripDropDownButton1 in the Form.

And I use this following logic which is working fine for me

private void DisableButton()

       {

           tbRef.Visible = false;

           tbGet.Visible = false;

           tbSave.Visible = false;

           tbEdit.Visible = false;

           tbDel.Visible = false;

           toolStripDropDownButton1.Visible = false;

       }

 

       private void GetControl()

       {

           this.Text = "Student Registeration";

           string form = this.Text;

           string name = null;

           string controls = null;

           foreach (DataGridViewRow items in dGVR.Rows)

           {

               string module = items.Cells[2].Value.ToString();

               string control = items.Cells[3].Value.ToString();

               bool status = Convert.ToBoolean(items.Cells[4].Value.ToString());

               name = module.ToString();

               controls = control.ToString();

               if (form == name)

                {

                   if (tbSave.Text == controls)

                   {

                       tbSave.Visible = true;

                   }

                   if (tbEdit.Text == controls)

                   {

                       tbEdit.Visible = true;

                   }

                   if (tbDel.Text == controls)

                   {

                       tbDel.Visible = true;

                   }

                   if (tbRef.Text == controls)

                   {

                       tbRef.Visible = true;

                   }

                   if (tbGet.Text == controls)

                   {

                       tbGet.Visible = true;

                   }

                   if (toolStripDropDownButton1.Text == controls)

                   {

                       toolStripDropDownButton1.Visible = true;

                   }

               }

           }

           if (form != name)

           {

               MessageBox.Show("Contact your Administrator", "Message", MessageBoxButtons.OK, MessageBoxIcon.Stop);

               this.Close();

           }

          

          

 

       }

 

 

It is working for me but I want to use the code like the following way

foreach (ToolStripDropDownItem item in menuStrip1.Items)

{

       item.Visible = false;

       if (item.Text == name)

                   {

                       item.Visible = true;

                   }  

 

 

}

Instead of

if (form == name)

                {

                   if (tbSave.Text == controls)

                   {

                       tbSave.Visible = true;

                   }

                   if (tbEdit.Text == controls)

                   {

                       tbEdit.Visible = true;

                   }

                   if (tbDel.Text == controls)

                   {

                       tbDel.Visible = true;

                   }

                   if (tbRef.Text == controls)

                   {

                       tbRef.Visible = true;

                   }

                   if (tbGet.Text == controls)

                   {

                       tbGet.Visible = true;

                   }

                   if (toolStripDropDownButton1.Text == controls)

                   {

                       toolStripDropDownButton1.Visible = true;

                   }

               }

Is there any solution for required output????


Posted 21 days ago Modified on 21 days ago

Hi smile,

Check this sample. now please take its reference.

Code

C#

Role.CS

static class Role
{
    public static string UserRole;
}

Form1

private void Form1_Load(object sender, EventArgs e)
{
    this.CenterToScreen();           
}

private void btnLogin_Click(object sender, EventArgs e)
{
    Role.UserRole = txtUser.Text.Trim();
    MainForm mainForm = new MainForm();
    mainForm.Show();
}

MainForm

private void MainForm_Load(object sender, EventArgs e)
{
    this.CenterToScreen();
    DataTable dt = new DataTable();
    dt.Columns.Add("G_Name");
    dt.Columns.Add("UserName");
    dt.Columns.Add("FormCaption");
    dt.Columns.Add("fCtrlName");
    dt.Columns.Add("IsRights");
    dt.Rows.Add("User", "User", "Student Inquiry", "View", 1);
    dt.Rows.Add("User", "User", "Student Registeration", "Save", 1);
    dt.Rows.Add("Admin", "Admin", "Student Attendance", "Update", 1);
    dt.Rows.Add("Admin", "Admin", "Stdent List", "Delete", 1);
    dt.Rows.Add("Admin", "Admin", "Student Promotion", "Export", 1);

    DataRow[] rows = dt.Select("G_Name='" + Role.UserRole.ToLower() + "'");

    foreach (ToolStripDropDownItem item in toolStripMenuItem.DropDownItems)
    {
        item.Visible = false;
        foreach (DataRow dr in rows)
        {
            if (dr["FormCaption"].ToString() == item.Text)
            {
                item.Visible = true;
                break;
            }
        }
    }
}

private void studentInquiryToolStripMenuItem_Click(object sender, EventArgs e)
{
    frmStuRegister form = new frmStuRegister();
    form.Show();
}

private void studentRegisterationToolStripMenuItem_Click(object sender, EventArgs e)
{
    frmStudentEnquiry form = new frmStudentEnquiry();
    form.Show();
}

private void studentAttendanceToolStripMenuItem_Click(object sender, EventArgs e)
{
    frmStudentAttendence form = new frmStudentAttendence();
    form.Show();
}

private void stdentListToolStripMenuItem_Click(object sender, EventArgs e)
{
    frmStudentList form = new frmStudentList();
    form.Show();
}

private void studentPromotionToolStripMenuItem_Click(object sender, EventArgs e)
{
    frmStudentPromotion form = new frmStudentPromotion();
    form.Show();
}

StudentRegisterForm

private void frmStuRegister_Load(object sender, EventArgs e)
{
this.CenterToScreen();
DataTable dt = new DataTable();
dt.Columns.Add("G_Name");
dt.Columns.Add("UserName");
dt.Columns.Add("FormCaption");
dt.Columns.Add("fCtrlName");
dt.Columns.Add("IsRights");
dt.Rows.Add("User", "User", "Student Inquiry", "View", 1);
dt.Rows.Add("User", "User", "Student Inquiry", "Save", 1);
DataRow[] rows = dt.Select("G_Name='" + Role.UserRole.ToLower() + "'");
foreach (DataRow dr in rows)
{
    string button = "btn" + dr["fCtrlName"].ToString();
    Control btn = this.Controls.Find(button, false)[0];
    btn.Visible = true;
}

StudentEnquiryForm

private void frmStudentEnquiry_Load(object sender, EventArgs e)
{
    this.CenterToScreen();
    DataTable dt = new DataTable();
    dt.Columns.Add("G_Name");
    dt.Columns.Add("UserName");
    dt.Columns.Add("FormCaption");
    dt.Columns.Add("fCtrlName");
    dt.Columns.Add("IsRights");
    dt.Rows.Add("User", "User", "Student Inquiry", "View", 1);
    dt.Rows.Add("User", "User", "Student Inquiry", "Save", 1);
    DataRow[] rows = dt.Select("G_Name='" + Role.UserRole.ToLower() + "'");
    foreach (DataRow dr in rows)
    {
        string button = "btn" + dr["fCtrlName"].ToString();
        Control btn = this.Controls.Find(button, false)[0];
        btn.Visible = true;
    }
}

StudentAttendenceForm

private void frmStudentAttendence_Load(object sender, EventArgs e)
{
    this.CenterToScreen();
    DataTable dt = new DataTable();
    dt.Columns.Add("G_Name");
    dt.Columns.Add("UserName");
    dt.Columns.Add("FormCaption");
    dt.Columns.Add("fCtrlName");
    dt.Columns.Add("IsRights");
    dt.Rows.Add("Admin", "Admin", "Student Attendance", "View", 1);
    dt.Rows.Add("Admin", "Admin", "Student Attendance", "Save", 1);
    dt.Rows.Add("Admin", "Admin", "Student Attendance", "Update", 1);
    dt.Rows.Add("Admin", "Admin", "Student Attendance", "Delete", 1);
    dt.Rows.Add("Admin", "Admin", "Student Attendance", "Export", 1);

    DataRow[] rows = dt.Select("G_Name='" + Role.UserRole.ToLower() + "'");

    foreach (DataRow dr in rows)
    {
        string button = "btn" + dr["fCtrlName"].ToString();
        Control btn = this.Controls.Find(button, false)[0];
        btn.Visible = true;
    }
}

StudentListForm

private void frmStudentList_Load(object sender, EventArgs e)
{
    this.CenterToScreen();
    DataTable dt = new DataTable();
    dt.Columns.Add("G_Name");
    dt.Columns.Add("UserName");
    dt.Columns.Add("FormCaption");
    dt.Columns.Add("fCtrlName");
    dt.Columns.Add("IsRights");
    dt.Rows.Add("Admin", "Admin", "Student List", "View", 1);
    dt.Rows.Add("Admin", "Admin", "Student List", "Save", 1);
    dt.Rows.Add("Admin", "Admin", "Student List", "Update", 1);
    dt.Rows.Add("Admin", "Admin", "Student List", "Delete", 1);
    dt.Rows.Add("Admin", "Admin", "Student List", "Export", 1);

    DataRow[] rows = dt.Select("G_Name='" + Role.UserRole.ToLower() + "'");
    foreach (DataRow dr in rows)
    {
        string button = "btn" + dr["fCtrlName"].ToString();
        Control btn = this.Controls.Find(button, false)[0];
        btn.Visible = true;
    }
}

StudentPromotionForm

private void frmStudentPromotion_Load(object sender, EventArgs e)
{
    this.CenterToScreen();
    DataTable dt = new DataTable();
    dt.Columns.Add("G_Name");
    dt.Columns.Add("UserName");
    dt.Columns.Add("FormCaption");
    dt.Columns.Add("fCtrlName");
    dt.Columns.Add("IsRights");
    dt.Rows.Add("Admin", "Admin", "Student Promotion", "Export", 1);

    DataRow[] rows = dt.Select("G_Name='" + Role.UserRole.ToLower() + "'");
    foreach (DataRow dr in rows)
    {
        string button = "btn" + dr["fCtrlName"].ToString();
        Control btn = this.Controls.Find(button, false)[0];
        btn.Visible = true;
    }
}

Screenshot