How To Create Login Form In Windows Application Using C#

Windows form

Windows form is used to build rich, interactive user interfaces in Windows applications.

Here I am creating a login page, the user will successfully login if user details are stored in database otherwise it will give an error message. I have used Visual Studio 2013 and sql server 2012 to build this application.

Here are the steps:

Step 1

First create the table and insert user login credentials.

Query for creating the table
  1. CREATE TABLE [dbo].[UserLogins](  
  2. [id] [int] IDENTITY(1,1) NOT NULL,  
  3. [UserName] [varchar](100) NULL,  
  4. [Password] [varchar](50) NULL,  
  5. CONSTRAINT [PK_UserLogins] PRIMARY KEY CLUSTERED  
  6. (  
  7.    [id] ASC  
  8. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]  
  9. ON [PRIMARY]   
 And then insert some values: username and password fields

Step 2

Create a Windows form application, using label, textbox and button from Toolbox .


Step 3 - on click Login button it will go in .cs file.

Here code for sql connection string and check for valid user. If log in is successful it will go to another page otherwise it will show an error message in the message box.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Data.SqlClient;  
  6. using System.Drawing;  
  7. using System.Linq;  
  8. using System.Text;  
  9. using System.Threading.Tasks;  
  10. using System.Windows.Forms;  
  11. namespace WindowsServiceSchedularApp {  
  12.     public partial class Form1: Form {  
  13.         public Form1() {  
  14.             InitializeComponent();  
  15.         }  
  16.         private void label1_Click(object sender, EventArgs e) {}  
  17.         private void button1_Click(object sender, EventArgs e) {  
  18.             //Connection String   
  19.             SqlConnection con = new SqlConnection(@ "Data Source=STL-4110;Initial Catalog=Practice;Integrated Security=True");  
  20.             SqlCommand cmd = new SqlCommand("select * from Userlogins where UserName=@UserName and Password =@Password", con);  
  21.             cmd.Parameters.AddWithValue("@UserName", textBox1.Text);  
  22.             cmd.Parameters.AddWithValue("@Password", textBox2.Text);  
  23.             SqlDataAdapter sda = new SqlDataAdapter(cmd);  
  24.             DataTable dt = new DataTable();  
  25.             sda.Fill(dt);  
  26.             //Connection open here   
  27.             con.Open();  
  28.             int i = cmd.ExecuteNonQuery();  
  29.             con.Close();  
  30.             if (dt.Rows.Count > 0) {  
  31.                 MessageBox.Show("Successfully loged in");  
  32.                 //after successful it will redirect  to next page .  
  33.                 WelcomePage settingsForm = new WelcomePage();  
  34.                 settingsForm.Show();  
  35.             } else {  
  36.                 MessageBox.Show("Please enter Correct Username and Password");  
  37.             }  
  38.         }  
  39.     }  
  40. }  
I have uploaded  application as  an attachment.
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now