Topics
Creating Registration and Login Page in ASP.Net
Introduction
In this article, I will explain, how to do registration and login using Asp.Net. I will use visual studio 2012 framework and SQL server 2008 R2 for this demo.
So, before opening visual studio let’s create and populate tblstudent and couple of store procedure using following script.
tblStudent table script
create table tblstudent ( FullName varchar(50), Address varchar(50), EmailID varchar(50), Password varchar(50) )
Stored Procedure – spregistration
create procedure spregistration ( @FullName varchar(40), @Address varchar(50), @EmailID varchar(20), @Password varchar(20) ) as insert into tblStudent values(@FullName,@Address,@EmailID, @Password)
Store Procedure – splogin
create PROCEDURE splogin ( @emailid as varchar(50), @password as varchar(50) ) AS SELECT * FROM tblStudent WHERE EmailID=@emailid AND Password=@password
Let’s start for this demo step by step using Asp.Net.
Step 1: Create Asp.Net project, by using following steps.
- Open Visual Studio.
- Click on File -> New -> Project.
- In this project, I am using C# as a programming language, but you can also choose VISUAL BASIC if you know syntaxes of VB.
- Inside Visual C# -> Web -> Website -> Select Asp.Net Empty Web site -> Name your project as loginandregistrationdemo -> Select Ok
Step 2 : After creating Asp.Net empty project, you will find project files inside ‘solution explorer’ on your right hand side.
- Open solution explorer
- Right click on your project name -> Add -> New Item
- Add new item – loginandregistrationdemo -> Add > Webform – > Rename the file to Registration
- Now Registration.aspx file is added to your solution explorer.
Step 3 : Write following code inside Registration.aspx and Index.aspx.cs files
- Copy and paste following code inbetween form element of your Registration.aspx.
<div> <table style="border:1px solid"> <tr> <td>Full Name </td> <td><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td> </tr> <tr> <td>Address </td> <td><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td> </tr> <tr> <td>Email ID </td> <td><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox></td> </tr> <tr> <td>Password </td> <td><asp:TextBox ID="TextBox4" runat="server" TextMode="Password"></asp:TextBox></td> </tr> <tr> <td> <asp:Button ID="btnregsitration" runat="server" Text="Submit" OnClick="btnregsitration_Click" /> </td> <td> <asp:Button ID="Button2" runat="server" Text="Login" OnClick="Button2_Click" /></td> <asp:Label ID="lblmsg" runat="server" Text="Label"></asp:Label> </tr> </table> </div>
- Now add following code inside Registration.aspx.cs file.
Registration.aspx.cs
protected void btnregsitration_Click(object sender, EventArgs e) { string strcon = "Data Source=.;database=sample;integrated security = true;"; SqlConnection con = new SqlConnection(strcon); SqlCommand com = new SqlCommand("spregistration", con); com.CommandType = System.Data.CommandType.StoredProcedure; SqlParameter sp1 = new SqlParameter("FullName", TextBox1.Text); SqlParameter sp2 = new SqlParameter("Address", TextBox2.Text); SqlParameter sp3 = new SqlParameter("EmailID", TextBox3.Text); SqlParameter sp4 = new SqlParameter("Password", TextBox4.Text); com.Parameters.Add(sp1); com.Parameters.Add(sp2); com.Parameters.Add(sp3); com.Parameters.Add(sp4); con.Open(); SqlDataReader dr = com.ExecuteReader(); con.Close(); lblmsg.Text = "Registration Successful"; lblmsg.Visible = true; } protected void Button2_Click(object sender, EventArgs e) { Response.Redirect("Login.aspx"); }
- Do not forget to import a namespaces.
using System.Data.SqlClient;
Step 4: Now let’s add create Login.aspx file
- To add login.aspx file inside your project(loginandregistrationdemo) -> Add > Webform – > Rename the file to Login
- Now add following code inside login.aspx file
<div> <table style="border:1px solid"> <tr> <td>Email ID </td> <td><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </td> </tr> <tr> <td>Password </td> <td><asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox> </td> </tr> <tr> <td> <asp:Button ID="btnlogin" runat="server" Text="Login" OnClick="btnlogin_Click" style="width: 47px" /> </td> <asp:Label ID="lblmsg" runat="server" Text="Label"></asp:Label> </tr> </table> </div>
- Add following code inside login.aspx.cs file
protected void btnlogin_Click(object sender, EventArgs e) { string strcon = "Data Source=.;database=sample;integrated security = true;"; SqlConnection con = new SqlConnection(strcon); SqlCommand com = new SqlCommand("login", con); com.CommandType = System.Data.CommandType.StoredProcedure; SqlParameter sp1 = new SqlParameter("EmailID", TextBox1.Text); SqlParameter sp2 = new SqlParameter("Password", TextBox2.Text); com.Parameters.Add(sp1); com.Parameters.Add(sp2); con.Open(); SqlDataReader dr = com.ExecuteReader(); if (dr.HasRows) { dr.Read(); lblmsg.Text = "Login successful."; lblmsg.Visible = true; } else { lblmsg.Text = "Invalid username or password."; lblmsg.Visible = true; } }
- Do not forget to import a namespaces.
using System.Data.SqlClient;
Step 5: Press F5 and see result
data repeat le raha hai