I am a beginner in C# database developing. I am developing a payroll application with four (4) user roles (i.e. Admin, Accountant, HR and ReadOnly).
There is a Dashboard that displays four buttons that link their respective access pages. Though when logged in based on the user's role, he is directed to the Dashboard successfully. Now, what am expecting are that when the Admin logs in, all buttons on the dashboard should be active. But whenever any of the other users logs in, all other buttons should be disabled, except his.
Below is the code I used; please can someone modify it for me to achieve my expectation?
private void loginButton_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
SqlCommand command = new SqlCommand();
con.Open();
string userText = userTextBox.Text;
string passText = pwdTextBox.Text;
SqlCommand cmd = new SqlCommand("select role from UsersLogin where UserName='" + userTextBox.Text + "'and Password='" + pwdTextBox.Text + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
MessageBox.Show("Welcome to the Dashboard");
this.Hide();
Dashboard dashboard = new Dashboard();
dashboard.Show();
cmd = new SqlCommand("SELECT Role from UsersLogin where Username=@Username", con);
cmd.Parameters.AddWithValue("@Username", userText);
string role = cmd.ExecuteScalar().ToString();
MessageBox.Show("Welcome " + role);
con.Close();
}
else
{
MessageBox.Show("Access Denied!!");
Application.Exit();
}
con.Close();
}
}