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:

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. userName = textBox1.Text;

  3. word = textBox2.Text;

  4. if (userName == "" && word == "")
    {
    MessageBox.Show("Please Enter Login Id and word");
    }
    else
    {
    connection();
    query = "Emplogin"; //stored Procedure Name
    com = new SqlCommand(query, con);
    com.CommandType = CommandType.StoredProcedure;
    com.Parameters.AddWithValue("@Usename", userName);
    com.Parameters.AddWithValue("@word", word);
    int usercount = (Int32)com.ExecuteScalar();
    if (usercount == 1)  //if user credential is matched then Usercount value is 1 otherwise it is 0
    {
    this.Hide(); //hiding form 1
    Main M1 = new Main(userName);//ing user Name to Second form so we can user Logged user Name
    M1.Show();
    }
    else
    {

  5. Login Lg = new Login();
    Lg.Show();

    MessageBox.Show("LoginId or word Is wrong"); //if user Name is not available in database

    }
    }

    }
 
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.

Up Next
    Ebook Download
    View all
    Learn
    View all