Login Form in Windows Application Using ASP.Net C#

Background

Identifying the user using authentication is a very important task in any application development and I have often read forum posts asking how to create a Windows Login form using a Stored Procedure. So by considering the preceding requirement I have decided to write this article to help beginners, students and anyone that wants to learn how to create a Windows Login form using a Stored Procedure.
So let us start with the walk through.
 
First create one table named "Emp_Login" in the SQL database that looks as in the following image:
 
CraeteTable.png
 
Insert a record in the table as:
  1. Insert into Emp_Login (userName,word) values ('vthal.wadje','vithal') 
In the preceding table the usename column is used to store the User Name and word is for the user's word.
 
Now let us create a Stored Procedure named "Emplogin" as follows: 
  1. Create procedure Emplogin  
  2. (  
  3. @Usename Varchar (20),  
  4. @word varchar (10)  
  5. )  
  6. as  
  7. Begin  
  8. Select COUNT(*)from Emp_Login where username=@Usename and word=@word  
  9. End 
In the preceding Stored Procedure named "Emplogin" the variable "@Usename" is used for the username and "@word" is used for the word. The select query that I have written in the beginning counts the number of users from the table whose name matches with the exact two variables; that is @Usename and @word that comes from the front end user input page.
 
Now create the project named Loginapplication or you can specify any name as:
  1. "Start" - "All Programs" - "Microsoft Visual Studio 2010".
  2. "File" - "New Project" - "C#" - Windows Application.
  3. Give the web site a name such as Loginapplication or another as you wish and specify the location.
  4. Then right-click on Solution Explorer then seelct  "Add windows forms", that is, Form1 for the Login Credentials and "Form2" for redirecting after successful login.
  5. Drag and drop two buttons, one Label and two textBoxes onto Form1 .
Then the Login Form1 design looks as in the following:
 
 
Then double-click on the Login button and write the following code in the Login_click function as:
  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3.     userName = textBox1.Text;  
  4.     word = textBox2.Text;  
  5.     if (userName == "" && word == "")  
  6.     {  
  7.         MessageBox.Show("Please Enter Login Id and word");  
  8.     }  
  9.     else  
  10.     {  
  11.         connection();  
  12.         query = "Emplogin"//stored Procedure Name  
  13.         com = new SqlCommand(query, con);  
  14.         com.CommandType = CommandType.StoredProcedure;  
  15.         com.Parameters.AddWithValue("@Usename", userName);  
  16.         com.Parameters.AddWithValue("@word", word);  
  17.         int usercount = (Int32)com.ExecuteScalar();  
  18.         if (usercount == 1)  //if user credential is matched then Usercount value is 1 otherwise it is 0  
  19.         {  
  20.             this.Hide(); //hiding form 1  
  21.             Main M1 = new Main(userName);//ing user Name to Second form so we can user Logged user Name  
  22.             M1.Show();  
  23.         }  
  24.         else  
  25.         {  
  26.             Login Lg = new Login();  
  27.             Lg.Show();  
  28.             MessageBox.Show("LoginId or word Is wrong"); //if user Name is not available in database  
  29.         }  
  30.     }  
  31. }
The most important part of the preceding code is the If condition part, I have used the one integer variable user countto count the single value from the Stored Procedure using "ExecuteScalar", if the records are found in the table then it returns 1 then the page is redirected to the Welcome page otherwise it gives Invalid user Name or word.
 
Now run the application.
our userName is- vithal.wadje
and word is - Vithal
 
That we are already entered into the table at the creation of the table. If you enter an invalid user name or word then it gives an error as shown in the following:
 
 
Now enter the valid username and word, that is:
UserName: Vithal.wadje
and word: vithal
 
Then the Form is redirected to the Main as shown in following:
 


Note
  • For detailed code please download the sample Zip file.
  • Perform the changes in the connectionStrings that is located in the app.config file depending on your server location.
Summary
 
From all of the preceding examples we have learned how to create a Windows Login form. I hope this article is useful for all readers. If you have a suggestion then please contact me.


Similar Articles