How to create Login and Registration Form in C# Windows Form With Database (2024)

Create Login and Registration Form in C# Windows Form With Database

Introduction

Hello guys, in this article we will create a login and registration form with database in c# windows form application .In this article we create a windows form app with login page, registration page for create new account and home page which show message that you login successfully .I hope you will like this article .So let’s start step by step.

How to create Login and Registration Form in C# Windows Form With Database (1)

Step: 1:-Open your visual studio, here I will use visual studio 2019.

Step: 2:-Clock on file menu on top of the visual studio, hover mouse on new and click on project.

How to create Login and Registration Form in C# Windows Form With Database (2)

Step: 3:-Search for windows form App.(.Net framework) and click on next.

How to create Login and Registration Form in C# Windows Form With Database (3)

Step: 4:-In this step you have to enter some details of your application and then click on Create button. Following details you have to enter.

How to create Login and Registration Form in C# Windows Form With Database (4)

1. Project Name:Name of your project

2. Location:Location where you want to store your app in your local computer.

3. Solution Name:This name is display in solution explore in visual studio.

4. Framework:Select appropriate framework as your application require.

Step: 5:-Now your project is created. Open Solution Explorer .If you don’t see solution explore you can open from View menu on top or you can try short cut key “Ctrl+W,S”. We need to create some pages for our application. Right click on solution name then Hover mouse on Add and click on Add New Item or you can user short cut key “Ctrl+Shift+A”.

How to create Login and Registration Form in C# Windows Form With Database (5)

Step: 6:-Now you see a dialog where we add our forms. Select Windows Form, give proper name and click on Add. Add Login, Registration and Home page in same way.

How to create Login and Registration Form in C# Windows Form With Database (6)

Step: 7:-Now we need to add database in our project. Right click on 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 left side bar for see item which associate with database. Select service based database, give name and click on add.

How to create Login and Registration Form in C# Windows Form With Database (7)

Step: 8:-Now we create a table which we user in login and registration. Double click on database file from solution explorer. It will open database file in server explore. Expand your database and right click on table then click on Add New Table.

How to create Login and Registration Form in C# Windows Form With Database (8)

Step: 9:-Create table field which you want, here I added only three field Id, UserName and password where id is auto increment by 1. You can set it by right click on field name , click on property and find Id Identity Specification expand it make true (Is Identity) field and give increment number which increment id by adding this number in last id.

How to create Login and Registration Form in C# Windows Form With Database (9)

CREATE TABLE [dbo].[LoginTable]( [Id] INT NOT NULL PRIMARY KEY IDENTITY, [username] NVARCHAR(50) NULL, [password] NVARCHAR(50) NULL)

Step:-10:-Now first of all we create Registration form. So create design of your form as you need. In below image you see how I design form.

How to create Login and Registration Form in C# Windows Form With Database (10)

Step: 11:-Now click anywhere on form it will generate Form_Load event where enter following code. This code create database connection and open it. In next step you will learn how you get that connection string which added in SQLConnection Constructor.

private void Registration_Load(object sender, EventArgs e){ cn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=H:\Website\RegistrationAndLogin\Database.mdf;Integrated Security=True"); cn.Open();}

Step: 12:-Go to server Explorer right click on database, click on Modify Connection.

How to create Login and Registration Form in C# Windows Form With Database (11)

Step: 13:-Now you see a windows dialog popup click on advance button. This will open another dialog. But Before that click on test button and check you database working properly.

How to create Login and Registration Form in C# Windows Form With Database (12)

Step: 14:-Copy path which show below on this dialog and close both dialog. Then past this path in form load event. Add @ sign before this path so you no need to change slash.

How to create Login and Registration Form in C# Windows Form With Database (13)

Step: 15:-We need to open login page when user click on login button so enter following code in Login Button click event.

private void Button1_Click(object sender, EventArgs e){ this.Hide(); Login login = new Login(); login.ShowDialog();}

Code Explanation:

1. Here 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 following code in registration button click event

private void BtnRegister_Click(object sender, EventArgs e) { if (txtconfirmpassword.Text != string.Empty || txtpassword.Text != string.Empty || txtusername.Text != string.Empty) { if (txtpassword.Text == txtconfirmpassword.Text) { cmd = new SqlCommand("select * from LoginTable where username='" + txtusername.Text + "'", cn); dr = cmd.ExecuteReader(); if (dr.Read()) { dr.Close(); MessageBox.Show("Username Already exist please try another ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { dr.Close(); cmd = new SqlCommand("insert into LoginTable values(@username,@password)", cn); cmd.Parameters.AddWithValue("username", txtusername.Text); cmd.Parameters.AddWithValue("password", txtpassword.Text); cmd.ExecuteNonQuery(); MessageBox.Show("Your Account is created . Please login now.", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information); } } else { MessageBox.Show("Please enter both password same ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show("Please enter value in all field.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }

Code Explanation:

1. First of all we check that user enter value in all field if yes that continue otherwise show message using message box.

2. Then we check if password and confirm password both are same.

3. Then we check if any record/user is already register with that username if not then continue further otherwise show error message.

4. In last we insert data in table using SQLCommand object.

Step: 17:-Now we create a login page here I add two textbox for username and password and two button for login and open registration form.

How to create Login and Registration Form in C# Windows Form With Database (14)

Step: 18:-Click on anywhere in form which generate Form_Load event add connection code that as show below.

private void Login_Load(object sender, EventArgs e){ cn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=H:\Website\RegistrationAndLogin\Database.mdf;Integrated Security=True"); cn.Open();}

Step: 19:-On Registration button click add following code which open registration form.

private void Btnregister_Click(object sender, EventArgs e){ this.Hide(); Registration registration = new Registration(); registration.ShowDialog();}

Step: 20:-Add below code in login button click for redirect user to home page form if user exist.

private void BtnLogin_Click(object sender, EventArgs e) { if (txtpassword.Text != string.Empty || txtusername.Text != string.Empty) { cmd = new SqlCommand("select * from LoginTable where username='" + txtusername.Text + "' and password='"+txtpassword.Text+"'", cn); dr = cmd.ExecuteReader(); if (dr.Read()) { dr.Close(); this.Hide(); Home home = new Home(); home.ShowDialog(); } else { dr.Close(); MessageBox.Show("No Account avilable with this username and password ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show("Please enter value in all field.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }

Code Explanation

1. Here first of all we check if user enter value in both field if yes then continue otherwise show error message.

2. Then we check if user exist in our database with that username and password. If user exist then open home page which we generate in start.

Step: 21:-Change start page as login in Program.cs File.

static void Main(){ Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Login());}

Step: 22:-Now run your application.

How to create Login and Registration Form in C# Windows Form With Database (15)

How to create Login and Registration Form in C# Windows Form With Database (16)

How to create Login and Registration Form in C# Windows Form With Database (17)

Conclusion

So, here we create a simple login and registration page in windows form application. I hope you like this article, share with your friends.

Source : My Website (YogeshHadiya.In) -> Create Login(Sign In) and Registration (Sign Up) Form in C# Windows Form With Database

In My C# Corner Blog

How to create Login and Registration Form in C# Windows Form With Database (2024)

FAQs

How to create login and registration form in C# Windows form with database? ›

Let's dive in step by step:
  1. Step 1: Creating a Windows Form Application. ...
  2. Step 2: Designing UI for Our Forms. ...
  3. Step 3: Adding Required Packages. ...
  4. Step 4: Preparing Our Database. ...
  5. Step 5: Coding Our Login Page. ...
  6. Step 6: Coding the Register Page. ...
  7. Step 7: Creating the Profile Page. ...
  8. Step 8: Testing Your Application.
Apr 5, 2024

How to create a login form in a C# Windows application? ›

How To Create Login Form In Windows Application Using C#
  1. First create the table and insert user login credentials. Query for creating the table. CREATE TABLE [dbo].[UserLogins]( ...
  2. Create a Windows form application, using label, textbox and button from Toolbox .
  3. Step 3 - on click Login button it will go in . cs file.
Dec 29, 2017

How to connect database to C# Windows form? ›

Follow the steps below for an easy C# SQL Server Database connection:
  1. Step 1: Install the Entity Framework NuGet Package.
  2. Step 2: Define your Data Model.
  3. Step 3: Create DbContext Class.
  4. Step 4: Configure Connection String.
  5. Step 5: Initialize Database.
  6. Step 6: Use DbContext in Your Code.
Feb 17, 2022

How to create a login form using SQL database? ›

Create a login using SSMS for SQL Server
  1. In Object Explorer, expand the folder of the server instance in which you want to create the new login.
  2. Right-click the Security folder, point to New, and select Login....
  3. In the Login - New dialog box, on the General page, enter the name of a user in the Login name box.
Aug 1, 2023

How to create a form in C# in Windows? ›

Visual Studio opens your new project.
  1. Open Visual Studio.
  2. On the start window, select Create a new project.
  3. In Create a new project, select the Windows Forms App (.NET Framework) template for C#. ...
  4. In the Configure your new project window, in Project name, enter HelloWorld, and select Create.
Mar 28, 2024

Does Windows Forms use C#? ›

C# Windows Forms is a graphical user interface (GUI) framework that enables developers to create desktop applications for the Windows operating system. Windows Forms applications are created using the C# programming language and the . NET framework.

How to set password in C# Windows form? ›

How to set the PasswordChar of the TextBox in C#?
  1. Step 1: Create a windows form. ...
  2. Step 2: Drag the TextBox control from the ToolBox and Drop it on the windows form. ...
  3. Step 3: After drag and drop you will go to the properties of the TextBox control to set the PasswordChar property of the TextBox.
Apr 20, 2023

How to use MySQL database in C# Windows application? ›

  1. Open connection to the database.
  2. Create a MySQL command.
  3. Assign a connection and a query to the command. ...
  4. Create a MySqlDataReader object to read the selected records/data.
  5. Execute the command.
  6. Read the records and display them or store them in a list.
  7. Close the data reader.
  8. Close the connection.

How to use DataSet in C# Windows form? ›

First, add the repository public variables and constructor. Add the DataSet Function - Add a Function GetData which takes the Sql Connection String and Stored Proc name + the ComboBox Department to execute the SqlCommand and Fill the DataAdapter with the DataSet for the Repository.

How to create Windows authentication login in SQL Server? ›

How to create a SQL server authentication login ID
  1. Run Microsoft SQL Server Management Studio.
  2. Expand the Security item in Object Explorer and right-click Logins and choose New Login….
  3. Enter an account name in the Login name field and choose SQL Server authentication.
Oct 20, 2015

How do I create a username and password for SQL database? ›

Step 3: Create a database user
  1. In SQL Server Management Studio, right-click Security > Logins; then select New Login.
  2. Enter the username (for example, papercut ).
  3. Change the Server Authentication to SQL Server and Windows Authentication mode.
  4. Enter the user's password.
  5. Disable password expiration.
  6. Click OK.

How do I add a SQL login to my database? ›

Creating a new SQL Login

Right-click on the Security folder under the database server and select New > Login.... In the Login - New dialog, you have the option of creating a new Login based on Windows authentication (for an existing Windows/Active Directory user) or a new Login based on SQL Server authentication.

How do I link a database to a registration form? ›

Now Just follow these simple steps and you will find that your first dynamic functional registration form with database entry on every form fill up.
  1. STEP 1: Create Database for inserting values. ...
  2. STEP 2: Front end code, Make HTML Structure of your registration form. ...
  3. STEP 3: For Database connectivity using MySQL.
Oct 26, 2013

How to create asp net login page using C# with SQL database? ›

How To Create Login Page In ASP.NET Web Application Using C# And SQL Server
  1. Creating a database and a table. To create a database, write the query in SQL Server. ...
  2. Let's start designing the login view in ASP.NET Web Application. ...
  3. Let's create a connection between the Web Application and SQL Server.
Oct 10, 2023

How to create a registration form in asp net with database? ›

For creating a registration form we have used one controller, two views, and one model class.
  1. First Create a Table in the Database (SQL Server 2012). ...
  2. Create a new project in Visual Studio 2015. ...
  3. ADD ENTITY DATA MODEL. ...
  4. CREATE A CONTROLLER. ...
  5. Add View. ...
  6. Add a new action into your controller for the get method.
May 13, 2024

References

Top Articles
Latest Posts
Article information

Author: Kimberely Baumbach CPA

Last Updated:

Views: 5627

Rating: 4 / 5 (41 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Kimberely Baumbach CPA

Birthday: 1996-01-14

Address: 8381 Boyce Course, Imeldachester, ND 74681

Phone: +3571286597580

Job: Product Banking Analyst

Hobby: Cosplaying, Inline skating, Amateur radio, Baton twirling, Mountaineering, Flying, Archery

Introduction: My name is Kimberely Baumbach CPA, I am a gorgeous, bright, charming, encouraging, zealous, lively, good person who loves writing and wants to share my knowledge and understanding with you.