INTRODUCTION
In this article, I will show you how to make a login form in Windows Application using C#. The log in page is connected with the database which contains the information about the usernames and their corresponding passwords.
STEP 1 Start the Project
Open Visual Studio 2017, go to file, new projects and under Visual C#, select windows form. You can change the name of the project to Login Form and press ok.
STEP2 Control Functions
In the corresponding Designer Page drag and drop buttons, labels, textbox, and panel from toolbox to form1.cs. You will see your form look like the following screenshot.
STEP3 - EXIT Button Click
Now make the code for the EXIT button. The form needs to be closed when Exit button is pressed so add this.close() to the Button code.
STEP4 - ADD NEW FORM
Add new form by right clicking the project title and save as Main.cs. Two forms are used, one form is for login page and the other form is for showing the message .
STEP5 - DATABASE CONNECTION
Follow this step:
- Create Database
- Modify web.config file
- The code to validate the user information
Database connections are made by right clicking the data connection-->add new or existing in data file name. If there is no existing file name create a new one. You can manage your database from the server explorer. In case it is not visible, then toggle it from the View Menu.
Write the name of your server the same as in your sql. When you provide the server name, go and select option, there will be the entire database name that you have made. You can create your new database too.
It will ask permission and click yes. Log on to server using Windows authentication or SQL authentication. You can see your Database in your Server Explorer.
STEP 6 - UPDATE TABLE
Go to your Database [Database.mdf], and we will create a table_login. Go to the database.mdf-Table then add new table, design the following table as seen in the below screenshot
The information from one form to another form is sent in the form of a SQL query to the database. Based on the result, actions are taken. Expand table and add new. Enter the detail and set primary key for the username; it cannot be null but for password it can be null, now data type for both is said to be varchar.
Now update the Table and update Database.
STEP 7 - CODING
After Database is created it's time to connect with the Form
Coding for login Button -- by right clicking the data connection, the properties of data connection will be opened and copy the connection detail and paste it in the code and the remaining code can be followed from the figure below.
Don't forget to add a library file and after that just debug your code.
STEP 8 - LOGIN DETAILS
Now update the Table detail for Login form (user name and password) and start to debug it. In the Output, enter the valid user name and password entered in the table. When the log in button is pressed the output will be displayed as shown (it will navigate to Main.cs page of FormApp).
When you give correct Username and password you will get to your dashboard.
STEP 9 - OUTPUT
For the wrong entry of user name and password, it will show an error message box.
Summary
This article showed you how to create a login page and connect it with the local database. Thank you so much for reading.