Sample code
Sample code
Step 1
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
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:
Location: Location where you want to store your app on your local computer.
Solution Name: This name is displayed in solution explorer in visual studio.
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
Databaseq
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.
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.
Now add the following code in the registration button click event:
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.
Then we check if the password and confirm password both are the same.
Then we check if any record/user is already registered with that username if not
then continue further otherwise show an error message.
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.
On a Registration button click, add the following code which opens the registration
form.
Add the below code in the login button click for redirecting users to the home page
form if the user exists.
}
else
{
MessageBox.Show("Please enter value in all field.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
C#
Code Explanation
Here, first of all, we check if the user enters a value in both fields. If yes,
then continue, otherwise, show an error message.
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
Login.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace RegistrationAndLogin
{
public partial class Login : Form
{
SqlCommand cmd;
SqlConnection cn;
SqlDataReader dr;
public Login()
{
InitializeComponent();
}
}
else
{
MessageBox.Show("Please enter value in all field.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
C#
Registration.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace RegistrationAndLogin
{
public partial class Registration : Form
{
SqlCommand cmd;
SqlConnection cn;
SqlDataReader dr;
public Registration()
{
InitializeComponent();
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace RegistrationAndLogin
{
public partial class Home : Form
{
public Home()
{
InitializeComponent();
}
}
}
C#
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace RegistrationAndLogin
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Login());
}
}
}