Make Login Form In Windows Application

In this article I will show you how to make Login Form in Windows Application using C# where I will enter a dummy data in the form of username and password and will check that the entered data by the user is correct or not. if it is correct, the user gets into his account.

Initial chamber

Step 1: Open Visual Studio 2010, Go to File, New, Projects and under Visual C#, select Windows.

browse your project

You can change the name of the project and browse your project to different location too. And then press OK.

Step 2: In Solution Explorer you get your Project, Add Service Based Database. By going to your Project, Right Click and Add New Item. After that select Service-based Database.

Service Based Database

Database chamber

Step 3: Get to your Database [Database.mdf], we will create a table tbl_login. Go to the database.mdf - Table, then Add New table, design your table like the following screenshot:


Tbl_login- show table data:

table data

Design chamber

Step 4: Now open Form1.cs[Design] file, where we create our design for Login Form Application.

We will drag two label and textbox and a button from the tool box to Form1.cs [Design], you will see your Form look like the following screenshot:


Code chamber

Right click on the blank part of Form1.cs - View Code. You will see you entered in the code part of the form. Write the following code and then Press F5 to run the project.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10. using System.Data.SqlClient;  
  13. namespace LoginFormWindowApplication  
  14. {  
  15.     public partial class Form1 : Form  
  16.     {  
  17.         public Form1()  
  18.         {  
  19.             InitializeComponent();  
  20.         }  
  22.         private void button1_Click(object sender, EventArgs e)  
  23.         {  
  24.             SqlConnection con = new SqlConnection(@"Data Source=NiluNilesh;Initial Catalog=mynewdata;Integrated Security=True");  
  25.             SqlCommand cmd = new SqlCommand("select * from tbl_login where username=@username and password =@password", con);  
  26.             cmd.Parameters.AddWithValue("@username", textBox1.Text);  
  27.             cmd.Parameters.AddWithValue("@password", textBox2.Text);  
  28.             SqlDataAdapter sda = new SqlDataAdapter(cmd);  
  30.             DataTable dt = new DataTable();  
  31.             sda.Fill(dt);  
  32.             con.Open();  
  33.             int i = cmd.ExecuteNonQuery();  
  34.             con.Close();  
  36.             if (dt.Rows.Count > 0)  
  37.             {  
  38.                 Welcome_Form settingsForm = new Welcome_Form();  
  39.                 settingsForm.Show();  
  40.             }  
  42.             else  
  43.             {  
  45.                 MessageBox.Show("Please enter Correct Username and Password");  
  46.             }  
  50.         }  
  51.     }  
  52. }  
Add one more from your Project and name it Welcome_Form.

Name it Welcome Form

Output chamber

When you give correct username and password you will get to your dashboard.

get to your dashboard


Otherwise if you enter wrong password:

enter wrong password

It will show an error message box:

error message box

Hope you liked it. Have a good day. Thank you so much for reading.


Similar Articles