News
IBM to offer mobile security as a service IBM will start delivering mobile security-as-a-service (MaaS) through its own cloud infrastructure in India, a move that is expected to better address in-country data requirements... IBM Spectrum Computing: IBM has enlarged its portfolio of software-defined infrastructure solutions with cognitive features for workload management.  * LiFi internet technology has been introduced, the new type of wireless internet connection that gives 100 times faster than traditional WiFi -- Invented by Professor Harald Haas from the University of Edinburgh. * Sci. Rachid Yazami has developed a smart chip that charges smartphones in less than 10 minutes. BenQ has launched BlueCore projector - Consumer electronics major BenQ has launched its first BlueCore laser light source projector. For those unaware, devices with BlueCore laser technology have a high contrast output of 80000:1 with an extended lamp life and efficiency. *** 
  Nov 9 2014 7:46AM     Rohan
  5 Comments    2855 Views  
Here Michael Ryan provided a short tutorial with example, how to create a login form in windows application using C#.
Database
For windows forms authentication, First have to create a table in Microsoft SQL Server Database as shown below.
Creating a table
When creating a table make Identity Specification as true for column 'ID'.

Set the table name as shown below.
Stored Procedure
Let us create a Stored Procedure named spLogin.

CREATE PROCEDURE spLogin
(
  @UserId VARCHAR(50),
  @Password VARCHAR(50)
)
AS
  BEGIN
  SELECT COUNT(*) FROM Emp_Login WHERE UserName=@UserId AND Password=@Password
  END

Form Design
Let us design a login form like this.
Namespaces
You will need to import the following namespace.

using System.Data; 
using System.Data.SqlClient;

Authentication using windows forms in C#
Below is the sample code for Authentication.
C#

if (UserName.Text != "" && Password.Text != "")
{
     string conString = "server= YourServerName; database=YourDatabase; uid=**; password=*****;";
     SqlConnection conn = new SqlConnection(conString);
     conn.Open();
     SqlCommand cmd = new SqlCommand("spLogin", conn);
     cmd.CommandType = CommandType.StoredProcedure;
     cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = UserName.Text;
     cmd.Parameters.Add("@Password", SqlDbType.VarChar).Value = Password.Text;
     int IsValidUser = Convert.ToInt32(cmd.ExecuteScalar());
     if (IsValidUser == 1) //if user found it returns 1
     {
           //Valid User
           MessageBox.Show("Success");
     }
     else
     {
           MessageBox.Show("InValid UserId Or Password");
     }
}
else
{
     MessageBox.Show("UserId and Password Is Required");
}

Result
I hope this page will helps to create a login form in Windows Forms Application. Thanks.
BackToTop
Comments

Micheal MC
Nov 9 2014 7:47AM
Thanks Nice Information
John L
Nov 9 2014 7:47AM
Many Thanks.. Working Fine...:)
Vishwanath
Nov 22 2014 3:33AM
Getting error in "conString" . What to do ?
Sumit Chan
Nov 22 2014 6:59AM
@Vishwanath, You might have mistake in database credentials. Make sure whether the credentials are correct. Check the server name, Database name, username and password.
Vishwanath
Nov 22 2014 11:19AM
@Sumit, Yes I made a mistake in server name part.. Thanks for your support...

 
Search
Recent Posts
Create Amazon ElasticCache Using Memcached in CSharp
Nov 21 2017 12:09PM Posted By Amose
Get Google Map Lat Lng (Geo Point) By Pincode in C#
Nov 8 2017 10:26AM Posted By Pranav
Google URL Shortener in C#
Oct 26 2017 10:43AM Posted By Sanjay
Bind Gridview from CSV file in Asp.Net C#
Jun 18 2017 11:38PM Posted By Michael
Call WebService method from jQuery in every 1 minute
Feb 27 2017 11:51AM Posted By John
Gridview custom CSS in ASP.Net
Feb 25 2017 10:59AM Posted By Micheal Ryan
Read excel file and bind to Gridview in C#
Feb 25 2017 10:20AM Posted By Micheal
Tags
Follow us on Facebook
Follow us on Google +
Recent post in your Email inbox.
Enter your email address: