Simple Windows Form Login Application in C#


We will learn how to create a Simple Windows application, We will design Login form and display User name in new form using database in mssql.

1. From File menu select new project.



1.Create a New Windows Form Application.



2.Design new Login form.





using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace LoginDemoApplication
{
    public partial class frmLogin : Form
    {
        // database connection string
        string connectionString = "Server=127.0.0.1;Database=DEMODB;uid=sa;pwd=password;";
        public frmLogin()
        {
            InitializeComponent();
        }
        private void btnLogin_Click(object sender, EventArgs e)
        {
            //validate your fields
            if (string.IsNullOrEmpty(txtUserName.Text))
            {
                MessageBox.Show("User Name can not be empty");
                txtUserName.Focus();
                return;
            }
            if (string.IsNullOrEmpty(txtPassword.Text))
            {
                MessageBox.Show("Password can not be empty");
                txtPassword.Focus();
                return;
            }
            //connection with database
            SqlConnection con = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand("Select * from UserMaster where UserName=@username", con);
            cmd.Parameters.AddWithValue("@username", txtUserName.Text);
            con.Open();
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            adapt.Fill(ds);
            con.Close();
            if (ds.Tables[0].Rows.Count == 0)
            {
                MessageBox.Show("Invalid User Name/Password");
                return;
            }
            //check password
            if (ds.Tables[0].Rows[0]["Password"].ToString() != txtPassword.Text)
            {
                MessageBox.Show("Invalid User Name/Password");
                return;
            }
            frmMaster.DisplayName = ds.Tables[0].Rows[0]["DisplayName"].ToString();
            frmMaster master = new frmMaster();
            this.Hide();
            master.Show();
        }
        private void btnClose_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        private void frmLogin_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }
    }
}



3.Design master page to view UserName after login.




        using System;
using System.Windows.Forms;
namespace LoginDemoApplication
{
    public partial class frmMaster : Form
    {
        public static string DisplayName;
        public frmMaster()
        {
            InitializeComponent();
        }
        private void btnLogout_Click(object sender, EventArgs e)
        {
            frmLogin login = new frmLogin();
            login.Show();
            this.Hide();
        }
        private void frmMaster_Load(object sender, EventArgs e)
        {
            lblUser.Text = DisplayName;
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        private void frmMaster_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }
    }
}

    


4.Create Table in mssql and insert Data.

Download Source
Login for comment