Creating Registration And Login Page In ASP.NET

We first open Visual Studio 2013 as administrator and create a new website. Create using File -> New -> Website. From the Visual C#  tab select the ASP.NET Empty Web Site and click on the OK button.


After creating the website the next step is to add a web form to this website. Right-click on the website and add a Web Form to the website.


Type the page name, whatever you want.


In the next section we design the ASP.NET page like that:


In the Design page we have used three labels, three textboxes and two buttons. One is for the Registration and the other one is for the Login. Now provide the name of labels and buttons, depending on your requirements.

Now create the table into the SQL Server database and take the three fields. The fields are username, password and email fields. The code looks as in this,

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

Stored Procedure for the table,

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

 We now double-click on the button and use the following code,

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

Now run the application.



Enter the Username, Email-id and Password and click on the Register Me Button.


 

For login we create an another webform and now right-click on the website.

Add a new form and provide the name, whatever you want.

The Design the form like this,


Now create a Stored Procedure for the login page as in the following,

  1. create PROCEDURE CUser  
  2. (  
  3.    @username as varchar(50),  
  4.    @password as varchar(50)  
  5. )  
  6. AS  
  7. SELECT * FROM registrationtab WHERE username=@username AND password=@password  
Now we double-click on the login button and write the following code,
  1. protected void Button1_Click(object sender, EventArgs e)  
  2. {  
  3.     string strcon = "Data Source=.;uid=sa;pwd=Password$2;database=roham";  
  4.     SqlConnection con = new SqlConnection(strcon);  
  5.     SqlCommand com = new SqlCommand("CUser", con);  
  6.     com.CommandType = System.Data.CommandType.StoredProcedure;  
  7.     SqlParameter p1 = new SqlParameter("username", TextBoxusername.Text);  
  8.     SqlParameter p2 = new SqlParameter("password", TextBoxpassword.Text);  
  9.     com.Parameters.Add(p1);  
  10.     com.Parameters.Add(p2);  
  11.     con.Open();  
  12.     SqlDataReader rd = com.ExecuteReader();  
  13.     if (rd.HasRows)  
  14.     {  
  15.         rd.Read();  
  16.         Label3.Text = "Login successful.";  
  17.         Label3.Visible = true;  
  18.     }  
  19.     else  
  20.     {  
  21.         Label3.Text = "Invalid username or password.";  
  22.         Label3.Visible = true;  
  23.     }  
  24. }    
Now run the application by pressing the F5 key.


 



 

I hope this article is helpful for the readers that want to simply create a registration and login page in ASP.NET. Thanks for reading this article, I hope you like it.