Wednesday, April 9, 2014

Simple Registration and Login form in Asp.NET

First create a table that contains 5 columns (ID, first_name,last_name,Email,Password)


Then add form and name it as StartPage.aspx and write down the following code


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="StartPage.aspx.cs" Inherits="StartPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
   
    <center style="font-family: 'Berlin Sans FB Demi'; font-size: xx-large; color: #FF0000">Welcome To ABC.com</center><br />
    <br />
    <br />
   
    <asp:Label ID="Label1" runat="server" Text="New User? Sign Up here"></asp:Label>
    &nbsp;
    <asp:Button ID="Button1" runat="server" ForeColor="#FF0066"
        onclick="Button1_Click" Text="SignUp" />
    <br />
    <br />
    <asp:Label ID="Label2" runat="server" Text="Already have an account?"></asp:Label>
&nbsp;&nbsp;
    <asp:Button ID="Button2" runat="server" ForeColor="#FF0066"
        onclick="Button2_Click" Text="LogIn" />
    </form>
</body>
</html>

and write the following code in StartPage.aspx.cs


protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("LogIn.aspx");
    }

Now create another form and name it as SignUp.aspx and write the following code in it


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 107px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <table style="width:27%;" align="center">
            <tr>
                <td class="style1">
                    First Name</td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
                
            </tr>
            <tr>
                <td class="style1">
                    Last Name</td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style1">
                    Email</td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                </td>
            </tr>
            </tr>
            <tr>
                <td class="style1">
                    Password</td>
                <td>
                    <asp:TextBox ID="TextBox4" runat="server" TextMode="Password"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style1">
                    &nbsp;</td>
                <td>
                    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="SignUp" 
                        ForeColor="#FF0066" />
                </td>
            </tr>
        </table>
    
    </div>
    </form>
</body>
</html>

now write the following code in SignUp.aspx.cs :-


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection cn;
    SqlCommand cmd;
    SqlDataAdapter da;

    protected void Page_Load(object sender, EventArgs e)
    {
        cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\C#\WebSites\Registration1\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        cmd = new SqlCommand("Insert into SignUp values(@first_name,@last_name,@Email,@Password)", cn);

        cmd.Parameters.AddWithValue("@first_name", TextBox1.Text);
        cmd.Parameters.AddWithValue("@last_name", TextBox2.Text);
        cmd.Parameters.AddWithValue("@Email", TextBox3.Text);
        cmd.Parameters.AddWithValue("@Password", TextBox4.Text);

        cn.Open();
        cmd.ExecuteNonQuery();
        cn.Close();
    }
}

Again add another web form and save it as LogIn.aspx. write there:-


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="LogIn.aspx.cs" Inherits="LogIn" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            height: 23px;
        }
        .style2
        {
            height: 23px;
            width: 101px;
        }
        .style3
        {
            width: 101px;
        }
        .style4
        {
            height: 23px;
            width: 130px;
        }
        .style5
        {
            width: 130px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <table align="center" style="width:50%;">
            <tr>
                <td class="style2">
                    Email</td>
                <td class="style4">
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rfvUser" ErrorMessage="Please enter Email ID" ControlToValidate="TextBox1" runat="server" />
                </td>
            <tr>
                <td class="style3">
                    Password </td>
                <td class="style5">
                    <asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rfvPWD" runat="server" ControlToValidate="TextBox2" ErrorMessage="Please enter Password"/>
                </td>
            </tr>
            <tr>
                <td class="style3">
                    &nbsp;</td>
                <td class="style5">
                    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
                </td>
            </tr>
        </table>
    
    </div>
    <br />
    <br />
    <br />
    <asp:Label ID="Label1" runat="server"></asp:Label>
    </form>
</body>
</html>

Now add following code in LogIn.aspx.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class LogIn : System.Web.UI.Page
{
    SqlConnection cn;
    SqlCommand cmd;
    SqlDataAdapter da;

    protected void Page_Load(object sender, EventArgs e)
    {
        cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\C#\WebSites\Registration1\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        da = new SqlDataAdapter("Select * from SignUp where Email=@Email and Password=@Password", cn);

        da.SelectCommand.Parameters.AddWithValue("@Email", TextBox1.Text);
        da.SelectCommand.Parameters.AddWithValue("@Password", TextBox2.Text);

        DataSet ds = new DataSet();
        da.Fill(ds);

        if (ds.Tables[0].Rows.Count != 0)
            Response.Redirect("Home.aspx");

        else
            Label1.Text = "Incorrect Email and Password";
    }
}

At last add another form and save it as HomePage.aspx :-


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    This is your home page....
    </div>
    </form>
</body>
</html>

Finish




No comments:

Post a Comment