Site icon ParallelCodes

C# Windows Form Creating Login form with SQL Database

In this post we will see how we can create C# Windows form Login page with MS SQL Database. I’m using Visual studio 2012 and MS SQL 2014 for developing this login form.

C# Windows Form Creating Login form with SQL Database – DOWNLOAD SOURCE CODE

Step 1:

Create a new Windows form application in Visual studio 2012 with name “WindowsLogin”.

Step 2:

In the project you will have a default form created with name “Form1”, in this form take two Windows form Labels in a row and name it (change their IDs) as “lblWelcome” and “lblInfo” respectively. And change the Windows name to “Dashboard”. This window form will serve as Dashboard or Homepage for our windows form application after user successfully logins into our c# application with valid Login userid and password. 

Open the code file and edit as below:

Form1.cs:

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;

namespace WindowsLogin
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
lblWelcome.Text = "Welcome " + UserInfo.userid;
lblInfo.Text = "Your EmpId:" + UserInfo.userid + ", UserId:" + UserInfo.userid + " and your Role:" + UserInfo.role;
}
}
}

Step 3:

Create a new class with name UserInfo.cs in our Windows form application and edit as below:

UserInfo.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsLogin
{
public class UserInfo
{
public static string userid = "";
public static string empid = "";
public static string role = "";

}
}

This class will be used for setting or providing the logged in User details in our application to all other forms.

Step 4:

In Program.cs add a static Boolean variable “openDashboard” and edit it as below

Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsLogin
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
///
public static Boolean openDashboard { get; set; }
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Login());

if (openDashboard)
{
Application.Run(new Form1());
}
}
}
}

We will use this variable to open another form after successful login process.

Step 5:

Create a new Window Form with name “Login” and make its design as below:

Login window:

Login Lock Image:

 In this form : 

Two Windows form Textboxes with id : txtUserId and txtPassword

Two Windows form Buttons with id : btnLogin and btnClear

A Windows form PictureBox for displaying Login Lock Image and three windows form labels, one for LOGIN FORM and other two for UserId and Password.

Double click on btnLogin and btnClear button to generate their Click methods.

Login form cs code:

Login.cs:

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 WindowsLogin
{
public partial class Login : Form
{
SqlConnection con;
SqlCommand cmd;
SqlDataReader reader;
public Login()
{
InitializeComponent();
}

private void btnLogin_Click(object sender, EventArgs e)
{
String result = "";
try
{
con = new SqlConnection(@"Data Source=192.168.0.192;Initial Catalog=Parallelcodes;User ID=sa;Password=789;Integrated Security=True");
cmd = new SqlCommand("select * from tblLogininfo where UserId=@uid and Password=@password", con);
con.Open();
cmd.Parameters.AddWithValue("@uid", txtUserId.Text.ToString());
cmd.Parameters.AddWithValue("@password", txtPassword.Text.ToString());
reader = cmd.ExecuteReader();
if (reader.Read())
{
if (reader["Password"].ToString().Equals(txtPassword.Text.ToString(), StringComparison.InvariantCulture))
{
UserInfo.empid = reader["EmpId"].ToString();
UserInfo.userid = reader["UserId"].ToString();
UserInfo.role = reader["UserRole"].ToString();
result = "1";
}
else
result = "Invalid credentials";
}
else
result = "Connection failed";

reader.Close();
cmd.Dispose();
con.Close();

}
catch (Exception ex)
{
result = ex.Message.ToString();
}

if (result == "1")
{
Program.openDashboard = true;
this.Close();
}
else
MessageBox.Show(result);
}

private void btnClear_Click(object sender, EventArgs e)
{
txtPassword.Text = "";
txtUserId.Text = "";
}
}
}

MS SQL Database script :

Create database ParallelCodes

USE [ParallelCodes]
GO

/****** Object: Table [dbo].[tblLoginInfo] Script Date: 1/12/2019 6:35:11 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[tblLoginInfo](
[Id] [int] IDENTITY(1,1) NOT NULL,
[EmpId] [int] NOT NULL,
[UserId] [nvarchar](50) NOT NULL,
[Password] [nvarchar](50) NOT NULL,
[UserRole] [nvarchar](10) NOT NULL,
[OnDate] [datetime] NULL DEFAULT (getdate())
) ON [PRIMARY]

GO

DOWNLOAD SOURCE CODE

Also see :

WPF Login form with MS SQL Database

ASP.NET Login form using Cookies

ASP.NET Login form using Sessions

 


Exit mobile version