Hi supriyan,
In order to do that you need to check the user role i.e. Admin or Operator.
If Admin then disable Button2 else enable both Buttons.
Refer below code.
User Role class
public static class UserRole
{
public static string Role { get; set; }
}
Registration Form
public partial class Registration : Form
{
public Registration()
{
InitializeComponent();
this.CenterToScreen();
}
private void btnRegister_Click(object sender, EventArgs e)
{
// Check your condition.
if (txtUserName.Text == "admin" && txtPassword.Text == "admin")
{
checkBox1.Checked = true;
checkBox2.Checked = false;
}
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "INSERT INTO User VALUES (@Name,@Password,@CheckBox1,@CheckBox2)";
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@Name", txtUserName.Text);
cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
cmd.Parameters.AddWithValue("@CheckBox1", checkBox1.Checked);
cmd.Parameters.AddWithValue("@CheckBox2", checkBox2.Checked);
con.Open();
// Insert in database.
cmd.ExecuteNonQuery();
con.Close();
}
Home frmHome = new Home();
frmHome.Show();
}
}
Login
private void btnLogin_Click(object sender, EventArgs e)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT Name FROM User WHERE Name = @Name AND Password = @password";
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
string name = Convert.ToString(cmd.ExecuteScalar());
con.Close();
if (!string.IsNullOrEmpty(name))
{
UserRole.Role = name.ToLower() == "admin" ? "admin" : "operator";
Home frmHome = new Home();
frmHome.Show();
}
}
}
Home Form
public partial class Home : Form
{
public Home()
{
InitializeComponent();
this.CenterToScreen();
button2.Enabled = true;
if (UserRole.Role.ToLower() == "admin")
{
button2.Enabled = false;
}
}
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
frm1.Show();
}
private void button2_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
}
}