Skip to content

ParallelCodes

  • AngularJS
  • Android
    • Android-Creating Notepad
    • Android Widgets Tutorial
    • Android Send SMS Programmatically with Permissions
    • Making Phone Calls in Android
    • Android JSON Parsing
    • Upload Files from Android to PC Programmatically
    • Android Interview Ques & Ans
  • ASP.NET
  • MVC
  • Xamarin
  • C#
  • WPF
  • CSS Templates

ParallelCodes

  • AngularJS
  • Android
    • Android-Creating Notepad
    • Android Widgets Tutorial
    • Android Send SMS Programmatically with Permissions
    • Making Phone Calls in Android
    • Android JSON Parsing
    • Upload Files from Android to PC Programmatically
    • Android Interview Ques & Ans
  • ASP.NET
  • MVC
  • Xamarin
  • C#
  • WPF
  • CSS Templates

C# Windows Form Creating Login form with SQL Database

  • by ASH
  • January 12, 2019March 12, 2020
  • 2 Comments
Post Views: 5,376

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

 


Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Telegram (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Skype (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to print (Opens in new window)
  • Click to share on Pocket (Opens in new window)
  • Click to email a link to a friend (Opens in new window)

Related

Tags:login formsql loginwindows formswinforms

2 thoughts on “C# Windows Form Creating Login form with SQL Database”

  1. Pingback: C# winforms login cant connecr to mysql (error 40) /login doesnt work - Tutorial Guruji

  2. Pingback: C# Windows Form - Creating PDF documents using iText7 library. - ParallelCodes

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Get all the latest updates for free on Facebook

Get all the latest updates for free on Facebook
 

C#, Windows Form Application
  • C# – Tuples
  • C# Array.Contains() – Incase Sensitive Search – Ignore Case
  • C# Windows Form – Creating PDF documents using iText7 library.
  • C# Windows Form Creating Login form with SQL Database
  • Use Custom Fonts in WPF C# applications
  • DataView Row Count after Filtering Data in C# ASP.NET
  • WPF Button Style with Rounded Corners and Hover Effects
  • WPF Textbox Style – Changing Colors on Focus
  • C# Filter DataTable by Date
  • C# Filter DataSet by Date
  • Creating Barcode Image in C#
  • Creating QRCode Image in C#
  • ASP.NET MVC Actions Example
  • ASP.NET MVC Controllers Overview
  • ASP.NET MVC Tutorial
  • ASP.NET File Upload control
  • Filtering Dataset in C#
  • ASP.NET Bind Gridview using Dataset
  • ASP.NET Bind Gridview from Database – MS SQL Database
  • ASP.NET Login page using Cookies
  • ASP.NET Login page using Sessions
  • ASP.NET Creating and Retrieving Sessions example
  • ASP.NET Creating and retrieving Sessions with MS SQL server
  • C# Windows Form Application Crystal Report Image from URL and Path.
  • ASP.NET Crystal Report Image from URL

Neve | Powered by WordPress