Create Login (Sign In) And Registration (Sign Up) Form In C# Windows Form With Database

Introduction

 
Hello guys, in this article we will create a login and registration form with a database in a C# Windows Form application. This application has three forms, login, registration, and home. Users first register themselves, then log in to their account and see the welcome message on the home page.
 
Step 1
 
Open your visual studio, here I will use Visual Studio 2019.
 
Step 2
 
The clock on file menu on top of the visual studio, hover mouse on new, and click on Project.
 
Create Login(Sign In) And Registration (Sign Up) Form In C# Windows Form With Database
 
Step 3
 
Search for Windows Form App (.NET framework) and click on next.
 
Create Login(Sign In) And Registration (Sign Up) Form In C# Windows Form With Database
 
Step 4
 
In this step, you have to enter some details of your application and then click on the Create button. You have to enter the following details:
  1. Project Name: Name of your project
  2. Location: Location where you want to store your app on your local computer.
  3. Solution Name: This name is displayed in solution explorer in visual studio.
  4. Framework: Select the appropriate framework as per your application requirements.
Create Login(Sign In) And Registration (Sign Up) Form In C# Windows Form With Database
 
Step 5
 
Now your project is created. Open Solution Explorer. If you don’t see solution explorer, you can open it from the View menu on top, or you can try the short cut key “Ctrl+W,S”. We need to create some pages for our application. Right-click on the solution name then Hover the mouse on Add and click on Add New Item, or you can user short cut key “Ctrl+Shift+A”.
 
Create Login(Sign In) And Registration (Sign Up) Form In C# Windows Form With Database
 
Step 6
 
Now you see a dialog where we add our forms. Select Windows Form, give it a proper name and click on Add. Add a Login, Registration, and Home page in the same way.
 
Create Login(Sign In) And Registration (Sign Up) Form In C# Windows Form With Database
 
Step 7
 
Now we need to add a database in our project. Right-click on the solution name, then Hover mouse on Add and click on Add New Item, or you can user short cut key “Ctrl+Shift+A”. Select data filter from the left sidebar to see the item which is associated with the database. Select service-based database, give it a name and click on add.
 
Create Login(Sign In) And Registration (Sign Up) Form In C# Windows Form With Database
 
Step 8
 
Now we create a table that we user in login and registration. Double click on the database file from solution explorer. It will open a database file in the server explorer.
 
Expand your database and right-click on the table, then click on Add New Table.
 
Create Login(Sign In) And Registration (Sign Up) Form In C# Windows Form With Database
 
Step 9
 
Create a table field that you want. Here, I added only three fields, ID, Username, and password, where ID is auto incremented by 1. You can set it by right clicking on the field name, click on property, and find the Id Identity Specification. Expand it and make it true (Is Identity) field and give an increment number which increments Id by adding this number in the last Id.
 
Create Login(Sign In) And Registration (Sign Up) Form In C# Windows Form With Database
  1. CREATE TABLE [dbo].[LoginTable]  
  2. (  
  3.     [Id] INT NOT NULL PRIMARY KEY IDENTITY,   
  4.     [username] NVARCHAR(50) NULL,   
  5.     [password] NVARCHAR(50) NULL  
  6. )  
Step 10
 
Now we create a Registration form. Create a design for your form as you need. In the below image, you see how I design a form.
 
Create Login(Sign In) And Registration (Sign Up) Form In C# Windows Form With Database
 
Step 11
 
Now click anywhere on the form. It will generate a Form_Load event where you can enter the following code. This code creates a database connection and opens it. In the next step, you will learn how you get that connection string which are added in SQLConnection Constructor.
  1. private void Registration_Load(object sender, EventArgs e)  
  2. {  
  3.             cn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=H:\Website\RegistrationAndLogin\Database.mdf;Integrated Security=True");  
  4.             cn.Open();  
  5. }  
Step 12
 
Go to Server Explorer, right-click on the database, then click on Modify Connection.
 
Create Login(Sign In) And Registration (Sign Up) Form In C# Windows Form With Database
 
Step 13
 
Now you see a windows dialog popup click on the advance button. This will open another dialog. Before that, click on the test button and check that your database is working properly.
 
Create Login(Sign In) And Registration (Sign Up) Form In C# Windows Form With Database
 
Step 14
 
Copy the path which shows below on this dialog and close both dialogs. Then paste this path in the form load event. Add @ sign before this path so there's no need to change the slash.
 
Create Login(Sign In) And Registration (Sign Up) Form In C# Windows Form With Database
 
Step 15
 
We need to open the login page when the user clicks on the login button, so enter the following code in the Login Button click event. 
  1. private void Button1_Click(object sender, EventArgs e)  
  2.   
  3.            this.Hide();  
  4.            Login login = new Login();  
  5.            login.ShowDialog();  
Code Explanation
  1. First, we hide the current form which is registration .
  2. Then we create an object of login page and show login form using that object.
Step 16
 
Now add the following code in the registration button click event:
  1. private void BtnRegister_Click(object sender, EventArgs e)  
  2. {  
  3.             if (txtconfirmpassword.Text != string.Empty || txtpassword.Text != string.Empty || txtusername.Text != string.Empty)  
  4.             {  
  5.                 if (txtpassword.Text == txtconfirmpassword.Text)  
  6.                 {  
  7.                     cmd = new SqlCommand("select * from LoginTable where username='" + txtusername.Text + "'", cn);  
  8.                     dr = cmd.ExecuteReader();  
  9.                     if (dr.Read())  
  10.                     {  
  11.                         dr.Close();  
  12.                         MessageBox.Show("Username Already exist please try another ""Error", MessageBoxButtons.OK, MessageBoxIcon.Error);  
  13.                     }  
  14.                     else  
  15.                     {  
  16.                         dr.Close();  
  17.                         cmd = new SqlCommand("insert into LoginTable values(@username,@password)", cn);  
  18.                         cmd.Parameters.AddWithValue("username", txtusername.Text);  
  19.                         cmd.Parameters.AddWithValue("password", txtpassword.Text);  
  20.                         cmd.ExecuteNonQuery();  
  21.                         MessageBox.Show("Your Account is created . Please login now.""Done", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  22.                     }  
  23.                 }  
  24.                 else  
  25.                 {  
  26.                     MessageBox.Show("Please enter both password same ""Error", MessageBoxButtons.OK, MessageBoxIcon.Error);  
  27.                 }  
  28.             }  
  29.             else  
  30.             {  
  31.                 MessageBox.Show("Please enter value in all field.""Error", MessageBoxButtons.OK, MessageBoxIcon.Error);  
  32.             }  
  33. }  
Code Explanation
  1. First of all, we check that the user entered a value in all fields. If yes, then continue, otherwise, show a message using the message box.
  2. Then we check if the password and confirm password both are the same.
  3. Then we check if any record/user is already registered with that username if not then continue further otherwise show an error message.
  4. In last we insert data in the table using the SQLCommand object.
Step 17
 
Now we create a login page. Here, I added two text boxes for username and password and two buttons for a login and open registration form.
 
Create Login(Sign In) And Registration (Sign Up) Form In C# Windows Form With Database
 
Step 18
 
Click on anywhere in a form which generates a Form_Load event add connection code, as shown below.
  1. private void Login_Load(object sender, EventArgs e)  
  2. {  
  3.             cn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=H:\Website\RegistrationAndLogin\Database.mdf;Integrated Security=True");  
  4.             cn.Open();  
  5. }   
Step 19
 
On a Registration button click, add the following code which opens the registration form.
  1. private void Btnregister_Click(object sender, EventArgs e)  
  2. {  
  3.             this.Hide();  
  4.             Registration registration = new Registration();  
  5.             registration.ShowDialog();  
  6. }  
Step 20
 
Add the below code in the login button click for redirecting users to the home page form if the user exists.
  1. private void BtnLogin_Click(object sender, EventArgs e)  
  2. {  
  3.             if (txtpassword.Text != string.Empty || txtusername.Text != string.Empty)  
  4.             {  
  5.   
  6.                 cmd = new SqlCommand("select * from LoginTable where username='" + txtusername.Text + "' and password='"+txtpassword.Text+"'", cn);  
  7.                 dr = cmd.ExecuteReader();  
  8.                 if (dr.Read())  
  9.                 {  
  10.                     dr.Close();  
  11.                     this.Hide();  
  12.                     Home home = new Home();  
  13.                     home.ShowDialog();  
  14.                 }  
  15.                 else  
  16.                 {  
  17.                     dr.Close();  
  18.                     MessageBox.Show("No Account avilable with this username and password ""Error", MessageBoxButtons.OK, MessageBoxIcon.Error);  
  19.                 }  
  20.   
  21.             }  
  22.             else  
  23.             {  
  24.                 MessageBox.Show("Please enter value in all field.""Error", MessageBoxButtons.OK, MessageBoxIcon.Error);  
  25.             }  
  26. }  
Code Explanation
  1. Here, first of all, we check if the user enters a value in both fields. If yes, then continue, otherwise, show an error message.
  2. Then we check if the user exists in our database with that username and password. If the user exists, then open the home page which we generated at the start.
Step 21
 
Change the start page as login in Program.cs File. 
  1. static void Main()   {
  2.            Application.EnableVisualStyles();  
  3.            Application.SetCompatibleTextRenderingDefault(false);  
  4.            Application.Run(new Login());  
  5.  }
Step 22
 
Now run your application.
 
 
 

Conclusion

 
So here, we created a simple login and registration page in a Windows Form application. I hope you liked this article, share it with your friends.