Simple User Login In ASP.NET Using C#

Introduction

In this article we will create a registration form, a login form and a form for checking for existing email. We will create a table and stored procedure in a SQL database.

Registration Form

Step 1 Create Table

Now create a table in a SQL Server database with username, password and email fields. The table looks like this.

  1. create table registrationtab  
  2. (  
  3.   Username varchar(100), Email varchar(100), Password varchar(20)  
  4. )  

Step 2 Create stored procedure

Now create a stored procedure for registration. That means the values will be inserted into the table using the stored procedure. The stored procedure looks like:

  1. create procedure[dbo].[storlogin134]  
  2. (  
  3.   @username varchar(40), @email varchar(50), @password varchar(20)  
  4. )  
  5. as  
  6. insert into registrationtab values(@username, @email, @password)  

Step 3 Create form for registration

Now create a form in ASP.Net with the following fields defined in the table. The form looks like the following figure.

change11.gif

Figure1

Now double-click on the register me button and add following code

  1. protected void Buttonregisterme_Click(object sender, EventArgs e) {  
  2.     string strcon = "Data Source=.;uid=sa;pwd=Password$2;database=master";  
  3.     SqlConnection con = new SqlConnection(strcon);  
  4.     SqlCommand com = new SqlCommand("storlogin134", con);  
  5.     com.CommandType = CommandType.StoredProcedure;  
  6.     SqlParameter p1 = new SqlParameter("username", TextBoxusername.Text);  
  7.     SqlParameter p2 = new SqlParameter("email", TextBoxemail.Text);  
  8.     SqlParameter p3 = new SqlParameter("password", TextBoxpassword.Text);  
  9.     com.Parameters.Add(p1);  
  10.     com.Parameters.Add(p2);  
  11.     com.Parameters.Add(p3);  
  12.     con.Open();  
  13.     com.ExecuteNonQuery();  
  14.     Labelinfo.Text = "registered successful.";  
  15. }  

Now run the application and enter the username, email and password and then click on the register me button to save the values to the database.

change12.gif

Figure 2

Open the database and check the registrationtab table.

Login form

Step 4 Create stored procedure

Now create a stored procedure for login. That means the values will be selected from the table using a stored procedure. The stored procedure looks like

  1. create PROCEDURE CheckUser  
  2. (  
  3.   @username as varchar(50), @password as varchar(50)  
  4. )  
  5. AS  
  6. SELECT * FROM registrationtab WHERE username = @username AND password = @password  

Now create a form in ASP.Net with the following fields defined in the table. The form looks like the following figure.



Figure3

Step 5 Create form for login

Now create a form in ASP.Net with the username and password field which are defined in the table. The form looks like the following figure.

Figure 4

Now double-click on the login button and add the following code.

  1. protected void Buttonlogin_Click(object sender, EventArgs e) {  
  2.     string strcon = "Data Source=.;uid=sa;pwd=Password$2;database=master";  
  3.     SqlConnection con = new SqlConnection(strcon);  
  4.     SqlCommand com = new SqlCommand("CheckUser", con);  
  5.     com.CommandType = CommandType.StoredProcedure;  
  6.     SqlParameter p1 = new SqlParameter("username", TextBoxusername.Text);  
  7.     SqlParameter p2 = new SqlParameter("password", TextBoxpassword.Text);  
  8.     com.Parameters.Add(p1);  
  9.     com.Parameters.Add(p2);  
  10.     con.Open();  
  11.     SqlDataReader rd = com.ExecuteReader();  
  12.     if (rd.HasRows) {  
  13.         rd.Read();  
  14.         Labelinfo.Text = "Login successful.";  
  15.     } else {  
  16.         Labelinfo.Text = "Invalid username or password.";  
  17.     }  
  18. }  

Now run the application and enter the username and password and then click on the login button to retrieve the values from the database. Now suppose we enter the wrong username and password, as in,


Figure4

Now enter the correct username and password,


Figure 5

check existence

Step6 Check email Existence

Now create a stored procedure for checking the existence of email. The stored procedure looks like:

  1. create PROCEDURE existance  
  2. (  
  3.   @email as varchar(50)  
  4. )  
  5. AS  
  6. SELECT * FROM registrationtab WHERE email = @email  

Now create a form in ASP.Net with the email field defined in the table. The form looks like the following figure.

check13.gif

Figure 6

Step-7

Now double-click on the login button and add the following code,

  1. protected void Buttonchekexistance_Click(object sender, EventArgs e) {  
  2.     string strcon = "Data Source=.;uid=sa;pwd=Password$2;database=master";  
  3.     SqlConnection con = new SqlConnection(strcon);  
  4.     SqlCommand com = new SqlCommand("existance", con);  
  5.     com.CommandType = CommandType.StoredProcedure;  
  6.     SqlParameter p1 = new SqlParameter("email", TextBoxemail.Text);  
  7.     com.Parameters.Add(p1);  
  8.     con.Open();  
  9.     SqlDataReader rd = com.ExecuteReader();  
  10.     if (rd.HasRows) {  
  11.         rd.Read();  
  12.         this.Labelinfo.ForeColor = System.Drawing.Color.Red;  
  13.         this.Labelinfo.Text = "already Exist!";  
  14.     } else {  
  15.         this.Labelinfo.Text = "you can login now";  
  16.         this.TextBoxusername.Text = "";  
  17.         this.TextBoxpassword.Text = "";  
  18.         this.TextBoxemail.Text = "";  
  19.     }  
  20. }  

Now run the application and enter the username, email and password to check for the existence of the emailid in the database. Suppose we enter a new email id,

check14.gif

Figure 7

Then click on the Check existence button.

check15.gif

Figure8

Now enter an existing email id and click on the Check existence button.

check16.gif

Figure 9

You can also download the attachment and test it yourself.

Up Next
    Ebook Download
    View all
    Learn
    View all