Advanced Login System

Introduction

In this article we will show you how to craete Window Login using Windows Application in Visual Studio using C#.

Prerequisites

Visual Studio 2010/2012/2013/15/17, SQL Server 2005/08/2012

Project used version

VS2017, SQL SERVER 2012

 Step 1: Go to -> File -> New -> Project

UK Academy

 Step 2: Select "Visual C#" as programming language, Select "Windows Forms Application", Project Name as "AdvancedLoginSystem" and Click OK Button.

AdvanceLoginSystem-VisualStudioProject

 Step 3: Rename form name "Form1" as "frmLogin".

AdvanceLoginSystem-RenameForm

 Step 4: Goto Project properties and Select "Setttings" tab in properties.

Note: To store User Information Temporary memory.

 Advance Login System-Project Setting

 Step 5: Enter the follwing entries in Name column UserLog and Email.

Note: To store User Information Temporary memory.

Advance Login System-Project Setting2

 Step 6: Goto add new item as shown below:

Advance Login System-Add New Form

 Step 7: Select LINQ to SQL Classes as shown below and Clikc Ok

Advance Login System-Data Class

 Step 8: Add new connection for LINQ to SQL Classes as shown below.

Advance Login System-LINQ to SQL

 

Step 9: Click Change Button to change Data Source to MS Sql Server as shown below:

Advance Login System-Data Source To MSSQL

Advance Login System-Select Server

 Step 10: Enter Server name, Select Log on Server as your prefered, Select Database name listed in combobox.

Advance Login System-Server Details

 Step 11: Expand newly created Data Source, Go to Stored Procedures and select spAuthentication, Drag stored procedure to LINQ to SQL Classes Panel

Advance Login System-Add Stroe Procedures

 Click Yes to Save password. Once drag you will see the spAuthentication Stored Procedure on right Panel.

Advance Login System-spAuthentication

 Step 12: Goto Login form Design, design form as shown below:

Login Form Design
Control  Text  Design Name
Label  Email ID lblEmailID
Label Password lblPassword
Text Box   txtEmailID
Text Box   txtPassword
Button Login btnLogin
Button Forget Password? btnForgetPassword
Button Exit btnExit
Group Box Login grbLogin

 

 Advance Login System-frmLogin Invalid 

 Step 13: Add new item "Window Form Application" name it as frmAdmin 

(If User role is Admin then show Admin Form) and frmEmployee (If User role is Employee then show Employee Form).

Advance Login System-Add Form

 Step 14: Declare public string variables after class as shown below:

Advance Login System-frm Add Forms

 Step 15: Double Click "btnLogin" that creates Button Click Event and Paste the follwing code

Advance Login System-btnLogin Code

Complete Source Code

 
using System;
using System.Windows.Forms;

namespace AdvancedLoginSystem
{
    public partial class frmLogin : Form
    {
        // Declare Global Variable Value
        public string result;
        public string role;
        public string userName;

        public frmLogin()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            // Establish connection with Database using LINQ to SQL Classes.
            using (DataClassesDataContext db = new DataClassesDataContext())
            {
                // spAuthentication() is stored procedure.
                // ref --- returns output values from stored procedure.
                db.spAuthentication(txtEmail.Text, txtPassword.Text, ref result, ref role, ref userName);
                db.SubmitChanges();
                //Popup Message Box
                MessageBox.Show(result.ToString());
                // If User Sucessfully Loged continues.
                if (result == "Logged Successfully!")
                {
                    //Hide current form
                    Hide();
                    //Store User Name temporary
                    AdvancedLoginSystem.Properties.Settings.Default.UserLog = userName;
                    AdvancedLoginSystem.Properties.Settings.Default.Save();
                    //Store Email Id temporary
                    AdvancedLoginSystem.Properties.Settings.Default.Email = txtEmail.Text;
                    AdvancedLoginSystem.Properties.Settings.Default.Save();
                    if (role == "Admin")
                    {
                        //If role is admin popup Administrator Home Form
                        frmAdmin AdminHome = new frmAdmin();
                        AdminHome.Show();
                    }
                    else if (role == "Employee")
                    {
                        //if role is Employee popup Employee Home form
                        frmEmployee empHome = new frmEmployee();
                        empHome.Show();
                    }
                }
            }
        }
    }
}

Step 16: Run Program..
Input Invalid Email and Password got returns error message "Invalid Email Id".

 Advance Login System-frmLogin In valid

Input Valid Email and Invalid Password then returns "Invalid Password, No of attemps 1 out of 3".

 Advance Login System-frmLogin-Invalid Password Attempt1

Retry with Input Valid Email and Invalid Password then returns "Invalid Password, No of attemps 2 out of 3".

Advance Login System-frmLogin-Invalid Password Attempt2

Input Valid Email and Valid Password then returns "Logged Successfully" as shown below:

Advance Login System-frmLogin-Successfully

If User role is Admin then show frmAdmin.

Advance Login System-Show frmAdmin

To Show current loged user. Drag and drop label from Toolbox and Double click frmAdmin.

Advance Login System-Admin Form Design

Paste the following code on form load. 

The following code load user name which is stored temporary.

Advance Login System-Admin Form Code

Advance Login System-frmAdmin 

 

Download Complete Source Code C#

 

Video Tutorial