public partial class MainForm : Form
{
int ctr; \\Give a class variable.Its will use for check the counter.
public MainForm()
{
InitializeComponent();
ctr =0; \\Initialize the 'ctr' which is variable at class form
}
private void button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=ARUSHRAJ-PC;Initial Catalog=sample;User ID=sa;Password=niit";
\\That is our data source from where application will retrieves the data.
con.Open();
SqlCommand cmd = new SqlCommand("Select * From Login_Details Where UserType=@ut AND UserName=@nm AND Password = @ps", con); \\This is a SQL comment which is define as you wish. But 1st you have to create a table in SQL Server.
cmd.Parameters.Add("@ut", SqlDbType.VarChar, 50);
cmd.Parameters.Add("@nm", SqlDbType.VarChar, 50); \\ Put the values as you have put in your database.
cmd.Parameters.Add("@ps", SqlDbType.VarChar, 50);
cmd.Parameters["@ut"].Value = comboBox1.Text;
cmd.Parameters["@nm"].Value = textBox1.Text; \\Give the path to retrieves the values which is given by user.
cmd.Parameters["@ps"].Value = textBox2.Text;
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows) \\HasRows is use for whatever dr store the data from Database & it will check though the given data by user.
{
DialogResult rd = MessageBox.Show("Login Succesfully", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
if (rd == DialogResult.OK)
{
\\Here you can write the next stage of your application.
Form2 obj = new Form2();
obj.Show();
if (dr.Read()) \\this condition use for that if Login successful message show then it will show on the Form that where user is logged-In. for this you can use blank Lable.
{
label6.Text = dr[1].ToString(); \\Put the correct array values.
label8.Text = dr[0].ToString();
}
}
}
else
{
ctr = ctr + 1; \\Here if you give the wrong password then counter will add 1 in class variable 'ctr'.
if (ctr < 3) \\Here application checking that, Is user have give 3 time wrong password or not. here you can give more or less time to check the counter,just put other values at 'IF(ctr<4)' as you wish.
{
DialogResult dr1 = MessageBox.Show("You have enter wrong Username or Password. If you are forget your Password or UserName ", "Warning", MessageBoxButtons.RetryCancel,MessageBoxIcon.Warning);
if (dr1 == DialogResult.Cancel)
{
Application.Exit();
}
linkLabel6.Show(); \\If user click on Retry button after give 1st time wrong password then a new
option will show on the form that is for FORGET PASSWORD. where user can see the correct password or Username. this code you can see on my Forget Password blog.
textBox1.Text = "";
textBox2.Text = "";
textBox1.Focus();
}
else
{
MessageBox.Show("You can not access this appliction.Because of you enter 3 times wrong Username or Password", "Stop", MessageBoxButtons.OK, MessageBoxIcon.Stop);
textBox1.Text = "";
textBox2.Text = "";
Application.Exit(); \\After show the message the application will auto close.
}
}
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
LoginForm
4645
18-Jul-2011
Anonymous User
13-Oct-2011Thanks Amit
John Smith
27-Jul-2011Keep Blogging...
John Smith
27-Jul-2011Amit Singh
27-Jul-2011Check this usefull link related to login control
http://www.mindstick.com/Articles/c389febe-fceb-4cb9-a4d1-41259346f449/?Login%20Form%20in%20ASP.Net
Arush Raj
22-Jul-2011