In many applications it is important to restrict the access to only registered users. We can also create different profiles for different users to restrict access. For example, if we are developing a school management system we could have these profiles:

  • Admin: View and do everything
  • Lecturer: Attendance and internal marks entry only
  • Accounts: Financial transactions only
  • Admissions: Student registration, Profile entry only.

In this article we are going to learn how to create a very simple login form using SQL database, the same principle can also be used for other databases like MS Access.

Step 1: Login Form

To begin, start a new project, ours we are going to name it LoginForm. Change the Name Properties of Form1 to “MainForm”. This will be the main from which will load only if the user is authenticated.

Right click LoginForm project in solution explorer then click “Add” then “New Item…”. Select Windows Form and name it “LoginForm”. This will be our login form which will pop up every time the application will start.

  • Change to ShowIcon properties to False: To remove the icon on the title bar of form.
  • Change MaximizeBox properties to False: To remove the Maximize box on the right upper corner of the form. This will prevent the user from maximizing the form.
  • Change MinimizeBox properties to False: To remove the Minimize box on the right upper corner of the form. This will prevent the user from minimizing the form.
  • Change the FormBorderStyle properties to FixedDiolog: To prevent user from resizing the form.
  • Drag and down two Labels (for username and password) and two text boxes and two Button (for Login and Exit) onto the login Window form, change the name properties of the text boxes to “txtUserName” and “txtPassword”.  Design the form as shown on figure below.

In PasswordChar properties of Password Text Box, insert ” * ” as shown on figure below to display this character when someone is typing his/her password.

Step 2: Database Connection

Right click LoginForm project in solution explorer again, then click “Add” then “New Item…”. Select Service-based Database to add an SQL Database into the project. Name it “Login”.

Click on the Server Explorer on the top left hand side, expend the Login.mdf Database, right click on the Tables and select “Add new Table” as shown on figure below.

Double click this new inserted Table to create columns. Create two columns, one “UserName” (Make this one our primary Key), set it Data type as “VarChar(50)” and uncheck Allow Nulls check button. Make the second column name “Password”, “VarChar(50)” Data type and check Allow Nulls check button. At the bottom under T-SQL tab, change the name of the table to “Users”. Click on “Update” on top to save the database.

On the Server Explorer again, expand the Tables again and right click our “Users” table and select “Show Table Data”. Add some few users and their passwords. In our case we are going to add two users as shown on the figure below.

On the Server Explorer, select the Login.mdf database and in the properties under Connection Strings, copy this connection string we are going to use it in the code.

When you copy this connection string, remove the project folder directory, as this can change depending on where you stored your project, your connection string should look like this:

“Data Source=(LocalDB)v11.0;Integrated Security=True;  AttachDbFilename=|DataDirectory|Login.mdf; Connect Timeout=30”

Here is the Main form code:

Here is the code for the Login Form:

You can download the full project files below here. All the files are zipped, you will need to unzip them (Download a free version of the Winzip utility to unzip files).  

Download LoginForm