0

I have used this code for login form in C# Windows Form application. also, I have created functions for different roles. How to get user role to main menu form and start run these functions? Please help me, sorry for my bad English. Are there any easy ways to create user authentication and hide menu items as user role (eg- Admin have full access, User only can access few buttons in main menu) in C#?

private void btnLogin_Click(object sender, EventArgs e) {
    try
    {
        if (txtUsername.Text == "" || txtPassword.Text == "")
        {
            MessageBox.Show("Please enter vaild Name & password", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
        }
    
        string uname, pass;
        uname = txtUsername.Text;
        pass = txtPassword.Text;
    
        sql = "SELECT * FROM tbl_Login WHERE username='" + uname + "' AND password='" + pass + "'";
        com = new SqlCommand(sql, con);
        con.Open();
        dreader = com.ExecuteReader();
        if (dreader.HasRows)
        {
            dreader.Read();
            string status = dreader["user_status"].ToString();
            if (status == "Active")
            {
                string type = dreader["user_role"].ToString();
                if (type == "Admin")
                {
                    FrmMainForm objMfrm = new FrmMainForm();
                    this.Hide();
                    objMfrm.Show();
                   
                                                
                }
                else if (type == "Clerk")
                {
                    
                   
    
                }
                else if (type == "Manager")
                {
                   
    
                }
                else if (status == "Deactive")
                {
                    MessageBox.Show("Your account has been disabled, please contact admin", "Expired Account", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    Application.Exit();
                }
    
            }
            else
            {
                MessageBox.Show("Invalid username or password", "Login Error");
            }
            
        }
    }
}

This is the code in main menu

public void clerk()
{
    btnAdminpn.Hide();
    btnDashboard.Hide();
    Reports.Hide();
}

public void manager()
{
    btnAdminpn.Hide();
}

public void cashier()
{
    btnAdminpn.Hide();
    btnDashboard.Hide();
    btnApartments.Hide();
    btnTenant.Hide();
    Reports.Hide();
    Maintenance.Hide();
}

So, How to get user role to main menu form and start run these functions? Please help me, sorry for my bad English. Are there any easy ways to creat User authentication and hide menu items as user role (eg- Admin have full access, User only can access few buttons in main menu) in C#?

Xavi Anguera
  • 105
  • 7
Isuru Abey
  • 37
  • 1
  • 8

0 Answers0