In this tutorial, we are going to discuss about how we can limit the user access in login window using C#.NET and SQL Server Database.

In many practical application we need to grand access to the user if they enter correct username and password. In case, if they forget their credentials(username and password) we need to give them chance to login to their account up to number of times generally three times is given. Beyond that we need to build such system which does not accept user input. So, this is far more reliable method because many times login attempt may causes the application crash and also an unauthorized person can try many times to get access to the user system. So that it is mandatory to limit the login form/window of an application.

STEPS:

1. Open your visual studio of any version and then go to file>new and then click on project. Then new window will appear and from the left pane select visual C#. under visual C# select window and then select window form application from the middle pane and give the name of your project and click OK.

2. Drag and drop two Label (for username, password), two TextBox (for letting user to input username and password) and one Button(For login in) from the toolbox and change the text and name of these control from the properties of these controls (To change text and name select each controls, right click>properties). Right click over your project from solution explorer and add new windows From and give name whatever you want.

 

This complete the designing part.

3. To create a database and table in SQL Server Database, open SQL Server application, and Right click over Database and then click new, give the database name whatever you want and click OK. After then expand that Database directory which you can see in the left pane of the SQL Server Studio Software and expand that Database directory and then expand your recently created Database. And then you will see Table directory. Right click that Table Directory click on Table and click OK. You will see the new window which looks like this:

 

and then give the column name such as username, password, set the data type  as varchar for each column. and save that table by pressing ctrl+s and give the table name whatever you want.

And then Right click your Table and then select Edit top 200 Rows click OK and then write the username, password  and click close button; this will save the username password and user type in your table.

4. We need to connect our application to the database for this, click on the Server Explore which is in the right side of the Toolbox in the left pane of your visual studio. And then right click on the Data Connection>Add connection. New window will appear select your Data Source; in our Case Data Source is Microsoft SQL Server. To do this click on change and select Microsoft SQL Server, Click OK. And then New window will appear, select your Server Name your Database Name and click OK.

5. Now Double Click in that login button and write the following code:

a. import the namespace at the top of your code.

using System.Data.SqlClient;

b. Declare one variable inside the class:

[charp]attempt=1;[/csharp]

c. Inside the clicking event of that login button write the following code:


if (attempt < 4)
{
SqlConnection con = new SqlConnection("Data Source=RAN-PC;Initial Catalog=loginattemptlimit;Integrated Security=True");
SqlCommand cmd = new SqlCommand("select * from loginattemptlimit where username='" + txtUser.Text + "' and password='" + txtPass.Text + "'", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
MessageBox.Show("logined successfully", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

Dashboard d = new Dashboard();
this.Hide();
d.Show();
}
else
{
MessageBox.Show("try again and the no. of attempt is " + attempt, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}else if (attempt ==4)
{
MessageBox.Show("logined attempt exceed", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
textBox1.Enabled = false;
textBox2.Enabled= false;
this.Close();

}
attempt++;
}

NOTE: Change this code in your context (because your connection string (Data Source = RAN-PC; Initial Catalog = loginattemptlimit; Integrated Security = True) is different than mine)

6. Run your project by click the green triangular button, input username and password and click login. If your provided input is match with the database value you will show successful message if not you will get chance to input 2 more times and then your application will close. And if you like this tutorial don’t forget to share this with your friends and write your perception about this article in your comment section. Thank you 🙂

If you have any confusion, you can watch the following videos:

 

SHARE