Tahera
Tahera

Reputation: 13

How to set user access by toolStripMenueItem in C# WinForm

I want to add in My project that admin can set access control to user.
Suppose admin can select user name and module name and save it to database.
When user successfully logged in he can only show the specific toolstripMenueItem which are added by Admin.

But I have no idea how to do it.

I simply add the user credentials and module name by admin. and when user try to log in I retrieve that control name into a string type variable, and try to do like this..

public void AccessControl()
{
    SqlConnection conn = new SqlConnection("Data Source=MYSOFTIT-05-PC\\SQLEXPRESS;Initial Catalog=Point_Of_Sale;Integrated Security=True");

    conn.Open();
    SqlCommand scmd = new SqlCommand("SELECT Control FROM Roll WHERE Email = 'taherak17@gmail.com'; ", conn);
    SqlDataAdapter dataAdapter = new SqlDataAdapter(scmd);
    DataTable dt = new DataTable();
    DataSet dataSet = new DataSet();
    dataAdapter.Fill(dataSet);
    SqlDataReader myReader = null;
    myReader = scmd.ExecuteReader();

    while (myReader.Read())
    {
        var a = (myReader[0].ToString());

    }
    conn.Close();
}

private void Login_Load(object sender, EventArgs e)
{
    AccessControl();
}

But the struck is how i define ToolStripMenuItem enabled or disabled according to database value? This is my Interface, and specific menu items are disable or enabled This is my Roll table

Upvotes: 0

Views: 329

Answers (1)

Sabri
Sabri

Reputation: 199

Save the ToolStripMenuItem Names in the database when the admin is authorizing the modules.Keep these menu items hidden by default. Then load the names from your query for example in an array. Then at parent form load do this.

        for (int i = 0; i < menuItems.Length; i++)
        {
            menuStrip.Items[menuItems[i]].Visible= true;
        }

Here menuItems is a string array which contains the ToolStripMenuItem names from your database. and menuStrip is your System.Windows.Forms.MenuStrip which contains ToolStripMenuItem(s). This will make visible only those MenuItems which the admin has authorized(stored in database).

Upvotes: 0

Related Questions