Loading, please wait...

A to Z Full Forms and Acronyms

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

Jun 07, 2021 c#, windows form, .net, win form app, , 50236 Views
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 log in successfully .

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.

 

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

 

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.

 

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 Create button. Following details you have to enter.

 

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

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”.

 

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 proper name and click on Add. Add Login, Registration and Home page in same way.

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

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.

 

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

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.

 

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

 

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.

Create Login(Sign In) and Registration (Sign Up) Form in C# Windows Form With Database
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.

 

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



 

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.

 

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 advance button. This will open another dialog. But Before that click on test button and check you database working properly.

 

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

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.

 

 

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

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.

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

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.

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

 

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

 

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

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

A to Z Full Forms and Acronyms

Related Article