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.

Database

Login table Query

CREATE TABLE tbl_login (

[ID] [int] IDENTITY(1,1) NOT NULL,

[username] [nvarchar](100) NOT NULL,

[password ] [nvarchar](100) NOT NULL,

CONSTRAINT [PK_tbl_login ] PRIMARY KEY CLUSTERED 

(

[ID] ASC

)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

) ON [PRIMARY]

Design

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

Code

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;
using System.Data.SqlClient;
namespace LoginFormWindowApplication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(@“Data Source=w3asp;Initial Catalog=logindata;Integrated Security=True”);
            SqlCommand cmd = new SqlCommand(“select * from tbl_login where username=@username and password =@password”, con);
            cmd.Parameters.AddWithValue(“@username”, textBox1.Text);
            cmd.Parameters.AddWithValue(“@password”, textBox2.Text);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            con.Open();
            int i = cmd.ExecuteNonQuery();
            con.Close();
            if (dt.Rows.Count > 0)
            {
                Welcome_Form settingsForm = new Welcome_Form();
                settingsForm.Show();
            }
            else
            {
                MessageBox.Show(“Please enter Correct Username and Password”);
            }
        }
    }
}

Need Help? Contact Us.

Log in

*
*

Forgot password?

*

New User

*
*
*
*