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
Step 2: Select "Visual C#" as programming language, Select "Windows Forms Application", Project Name as "AdvancedLoginSystem" and Click OK Button.
Step 3: Rename form name "Form1" as "frmLogin".
Step 4: Goto Project properties and Select "Setttings" tab in properties.
Note: To store User Information Temporary memory.
Step 5: Enter the follwing entries in Name column UserLog and Email.
Note: To store User Information Temporary memory.
Step 6: Goto add new item as shown below:
Step 7: Select LINQ to SQL Classes as shown below and Clikc Ok
Step 8: Add new connection for LINQ to SQL Classes as shown below.
Step 9: Click Change Button to change Data Source to MS Sql Server as shown below:
Step 10: Enter Server name, Select Log on Server as your prefered, Select Database name listed in combobox.
Step 11: Expand newly created Data Source, Go to Stored Procedures and select spAuthentication, Drag stored procedure to LINQ to SQL Classes Panel
Click Yes to Save password. Once drag you will see the spAuthentication Stored Procedure on right Panel.
Step 12: Goto Login form Design, design form as shown below:
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 |
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).
Step 14: Declare public string variables after class as shown below:
Step 15: Double Click "btnLogin" that creates Button Click Event and Paste the follwing 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".
Input Valid Email and Invalid Password then returns "Invalid Password, No of attemps 1 out of 3".
Retry with Input Valid Email and Invalid Password then returns "Invalid Password, No of attemps 2 out of 3".
Input Valid Email and Valid Password then returns "Logged Successfully" as shown below:
If User role is Admin then show frmAdmin.
To Show current loged user. Drag and drop label from Toolbox and Double click frmAdmin.
Paste the following code on form load.
The following code load user name which is stored temporary.