Samuel Toh

Samuel Toh

  • NA
  • 106
  • 16.7k

Restrict certain Users from accessing pages in Windows Form App C#

Oct 28 2021 8:20 AM

private void btnLogin_Click(object sender, EventArgs e)
{
    if (txtpassword.Text != string.Empty || txtusername.Text != string.Empty)
    {
        
        cmd = new SqlCommand("select * from Login where Username='" + txtusername.Text + "' and Password='" + txtpassword.Text + "'", cn);
        dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            dr.Close();
            this.Hide();
            Home home = new Home();
            home.ShowDialog();
        }
        else
        {
            dr.Close();
            MessageBox.Show("No Account available with this Username and Password ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

    }
    else
    {
        MessageBox.Show("Please enter value in all field.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

From the above picture, the left side has 6 Buttons. "Login", "Registration", "Home", "About", "Job Application" & "Admin". What I want to achieve is that only normal Users can access all the buttons except "Admin" button, and only Admin uses can access "Admin" button. The codes I have typed below the picture is the code for the login page. During the Login page, no one is allowed to go to any of the pages after clicking the button unless they log in.

So there are two things that I want to achieve:

1. Only normal Users can access all the buttons except "Admin" button, and only Admin uses can access "Admin" button.

2. During the Login page, no one is allowed to go to any of the pages after clicking the button unless they log in.


Answers (2)