Hi my name is vishal.I have a application named:
Project which has mdi parent form named:
MDIParent1. I have a login form named:
frmLogin. Given below is my c# code of frmLogin with sql server 2008.:
using System.Data.SqlClient;
namespace Project
{
public partial class frmLogin : Form
{
public frmLogin()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
if ((txtPassword.Text == "tuscano") && (txtUsername.Text.ToLower() == "kumar"))
{
MDIParent1 g = new MDIParent1();
g.Show();
this.Close();
}
else
{
string username = txtUsername.Text;
string password = txtPassword.Text;
bool validUser = ValidateUser(username, password);
if (validUser)
{
MDIParent1 m = new MDIParent1();
m.Show();
this.Close();
}
else
{
MessageBox.Show("Invalid user name or password. Please try with another user name or password", "DRRS", MessageBoxButtons.OK, MessageBoxIcon.Warning);
txtUsername.Focus();
}
}
}
private bool ValidateUser(string username, string password)
{
bool success = false;
SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=DRRS;Integrated Security=true");
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd = new SqlCommand("Select @count=Count(*) from [dbo].[User] where username=@username and password=@password", conn);
cmd.Parameters.AddWithValue("@username", txtUsername.Text);
cmd.Parameters.AddWithValue("@password", txtPassword.Text);
cmd.Parameters.Add("@count", SqlDbType.Int).Direction = ParameterDirection.Output;
conn.Open();
cmd.ExecuteNonQuery();
if (Convert.ToInt32(cmd.Parameters["@count"].Value) > 0)
{
success = true;
}
else
{
success = false;
}
conn.Close();
return success;
}
The above c# code Works with no problem at all!
However what is want is to restrict a user after
3 unsuccessful attempts(failed logins/attempts) by displaying a message box to user telling
"Sorry you have tried to access application either with wrong user name or wrong password! Sorry for the trouble!". Can anyone help me/guide me on how to achieve this required result?! Can anyone help me Please! Any help/guidance in solving of this problem would be greatly appreciated!