I'm having hard time trying to solved it and tried many times but still it doesn't work. Here's the scenario, I have a login form which have username and password. I have a database for creating user, users have type admin and employee. What i want to happen is to get the username of the user and the type of user and pass it to a label in another form.
Here's my code
private static int count = 0;
private void btn_login_Click(object sender, EventArgs e)
{
using (var con = SQLConnection.GetConnection())
{
var selectCommand = new SqlCommand("Select * from Users_Profile where Username= @Username and Password= @Password", con);
selectCommand.Parameters.Add("@Username", SqlDbType.VarChar, 50).Value = txt_username.Text;
selectCommand.Parameters.Add("@Password", SqlDbType.VarChar, 50).Value = txt_password.Text;
SqlDataReader dataReader;
dataReader = selectCommand.ExecuteReader();
var loginSuccess = false;
while (dataReader.Read())
{
loginSuccess = true;
}
if (string.IsNullOrEmpty(txt_username.Text) || string.IsNullOrEmpty(txt_password.Text))
{
MetroMessageBox.Show(this, "Please input the Required Fields", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
if (loginSuccess)
{
count = 0;
MetroMessageBox.Show(this, "Login Successful", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Hide();
var obj = new MainForm(this);
obj.Closed += (s, args) => this.Close();
obj.Show();
}
else
{
count += 1;
if (count == 3)
{
MetroMessageBox.Show(this, "You have exceeded maximum login attempts, Please wait 10 seconds", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Stop);
txt_username.Enabled = false;
txt_password.Enabled = false;
btn_login.Enabled = false;
LoginAttempstimeOut.Start();
}
else
{
MetroMessageBox.Show(this, "Invalid Username/Password", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
}
}
}
}
private void LoginAttempstimeOut_Tick(object sender, EventArgs e)
{
LoginAttempstimeOut.Stop();
txt_username.Enabled = true;
txt_password.Enabled = true;
btn_login.Enabled = true;
count = 0;
}