Create SignUp Form With Login Form And Database In ASP.NET Web Forms

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


<!DOCTYPE html>


<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

    <style type="text/css">

        .auto-style2 {

             text-align:center;

             font-size:30px;

        }

        table

        {

            margin:auto;

            width:300px;

            border:5px black ridge;

        }

        .link 

        {

            text-align:center;

        }

    </style>

</head>

<body>

    <form id="form1" runat="server">

        <div>

            <table cellpadding="4" cellspacing="4">

                <tr>

                    <td class="auto-style2" colspan="2">LOGIN</td>

                </tr>

                <tr>

                    <td>USERNAME</td>

                    <td>

                        <asp:TextBox ID="UserTextBox" runat="server"></asp:TextBox>

                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="UserTextBox" Display="Dynamic" ErrorMessage="Please Enter Username" ForeColor="Red" SetFocusOnError="True"></asp:RequiredFieldValidator>

                    </td>

                </tr>

                <tr>

                    <td>PASSWORD</td>

                    <td>

                        <asp:TextBox ID="PassTextBox" runat="server" TextMode="Password"></asp:TextBox>

                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="PassTextBox" Display="Dynamic" ErrorMessage="Please Enter Password" ForeColor="Red" SetFocusOnError="True"></asp:RequiredFieldValidator>

                    </td>

                </tr>

                <tr>

                    <td class="auto-style2">&nbsp;</td>

                    <td>

                        <asp:Button ID="LoginButton" runat="server" Height="40px" Text="LOGIN" Width="96px" OnClick="LoginButton_Click" />

                    </td>

                </tr>

                <tr>

                    <td class="link" colspan="2">

                        <a href="REGISTER.aspx">Not Registered yet ? Click Here</a>

                    </td>

                </tr>

            </table>

        </div>

    </form>

</body>

</html>

Above File is LOGIN_FORM_ASP\LOGIN.aspx








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

namespace LOGIN_FORM_ASP
{
    public partial class LOGIN : System.Web.UI.Page
    {
        string cs = ConfigurationManager.ConnectionStrings["dbcs2"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void LoginButton_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(cs);
            string query = "select * from signup where username=@user and password=@pass";
            SqlCommand cmd = new SqlCommand(query,con);
            cmd.Parameters.AddWithValue("@user",UserTextBox.Text);
            cmd.Parameters.AddWithValue("@pass",PassTextBox.Text);
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                Session["user"] = UserTextBox.Text;
                //Page.ClientScript.RegisterStartupScript(this.GetType(), "Scripts", "<script>alert('Login Successful !!')</script>");
                Response.Redirect("DASHBOARD.aspx");
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(),"Script","<script>alert('Login Failed !!')</script>");
            }
            con.Close();
        }
    }
}
Above File is LOGIN_FORM_ASP\LOGIN.aspx.cs








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

namespace LOGIN_FORM_ASP
{
    public partial class DASHBOARD : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] != null)
            {
                Response.Write("Welcome to my site Mr/Mrs " + Session["user"].ToString());
            }
            else
            {
                Response.Redirect("LOGIN.aspx");
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Session["user"] != null)
            {
                Session["user"] = null;
                Response.Redirect("LOGIN.aspx");
            }
        }
    }
}
Above File is LOGIN_FORM_ASP\DASHBOARD.aspx.cs








<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="REGISTER.aspx.cs" Inherits="LOGIN_FORM_ASP.REGISTER" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width:500px;
            margin:auto;
            border:ridge 5px black;
        }
        .auto-style2 {
            width: 152px;
        }
        .auto-style3 {
            width: 152px;
            height: 31px;
        }
        .auto-style4 {
            height: 31px;
            width: 306px;
        }
        .auto-style5 {
            text-align:center;
            font-size:30px;
        }
        .auto-style6 {
            width: 306px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table cellpadding="3" cellspacing="3" class="auto-style1">
                <tr>
                    <td class="auto-style5" colspan="2">SIGNUP FORM</td>
                </tr>
                <tr>
                    <td class="auto-style2">FIRST NAME</td>
                    <td class="auto-style6">
                        <asp:TextBox ID="FirstNameTextBox" runat="server" Width="223px"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" Display="Dynamic" ErrorMessage="Please Enter First Name" ForeColor="Red" SetFocusOnError="True" ControlToValidate="FirstNameTextBox">*</asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style2">LAST NAME</td>
                    <td class="auto-style6">
                        <asp:TextBox ID="LastNameTextBox" runat="server" Width="223px"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" Display="Dynamic" ErrorMessage="Please Enter Last Name" ForeColor="Red" SetFocusOnError="True" ControlToValidate="LastNameTextBox">*</asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style2">GENDER</td>
                    <td class="auto-style6">
                        <asp:DropDownList ID="DropDownList1" runat="server" Width="231px">
                            <asp:ListItem>Select</asp:ListItem>
                            <asp:ListItem>Male</asp:ListItem>
                            <asp:ListItem>Female</asp:ListItem>
                        </asp:DropDownList>
                        <asp:RequiredFieldValidator InitialValue="Select" ID="RequiredFieldValidator3" runat="server" Display="Dynamic" ErrorMessage="Please Select a gender" ForeColor="Red" SetFocusOnError="True" ControlToValidate="DropDownList1">*</asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style2">EMAIL</td>
                    <td class="auto-style6">
                        <asp:TextBox ID="EmailTextBox" runat="server" Width="223px"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" Display="Dynamic" ErrorMessage="Please Enter Email" ForeColor="Red" SetFocusOnError="True" ControlToValidate="EmailTextBox">*</asp:RequiredFieldValidator>
                        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="EmailTextBox" Display="Dynamic" ErrorMessage="Please Enter Valid Email" ForeColor="Red" SetFocusOnError="True" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style2">ADDRESS</td>
                    <td class="auto-style6">
                        <asp:TextBox ID="AddressTextBox" runat="server" Width="223px"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" Display="Dynamic" ErrorMessage="Please Enter Address" ForeColor="Red" SetFocusOnError="True" ControlToValidate="AddressTextBox">*</asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style3">USERNAME</td>
                    <td class="auto-style4">
                        <asp:TextBox ID="UserNameTextBox" runat="server" Width="223px"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" Display="Dynamic" ErrorMessage="Please Enter Username" ForeColor="Red" SetFocusOnError="True" ControlToValidate="UserNameTextBox">*</asp:RequiredFieldValidator>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style3">PASSWORD</td>
                    <td class="auto-style4">
                        <asp:TextBox ID="PasswordTextBox" runat="server" Width="223px"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" Display="Dynamic" ErrorMessage="Please Enter Password" ForeColor="Red" SetFocusOnError="True" ControlToValidate="PasswordTextBox">*</asp:RequiredFieldValidator>
                        <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="PasswordTextBox" Display="Dynamic" ErrorMessage="PLEASE ENTER A STRONG PASSWORD WITH UPPERCASE, LOWERCASE, NUMBERS AND CHARACTERS" SetFocusOnError="True" ValidationExpression="(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$">*</asp:RegularExpressionValidator>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style3">CONFIRM PASSWORD</td>
                    <td class="auto-style4">
                        <asp:TextBox ID="ConfirmPasswordTextBox" runat="server" Width="223px"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" Display="Dynamic" ErrorMessage="Please Enter Confirm Password" ForeColor="Red" SetFocusOnError="True" ControlToValidate="ConfirmPasswordTextBox">*</asp:RequiredFieldValidator>
                        <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="PasswordTextBox" ControlToValidate="ConfirmPasswordTextBox" Display="Dynamic" ErrorMessage="Password is not Identical" ForeColor="Red" SetFocusOnError="True">*</asp:CompareValidator>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style3">&nbsp;</td>
                    <td class="auto-style4">
                        <asp:Button ID="Button1" runat="server" Height="40px" Text="SIGNUP" Width="85px" OnClick="Button1_Click" />
                    </td>
                </tr>
                <tr>
                    <td class="auto-style3">&nbsp;</td>
                    <td class="auto-style4">
                        <a href="LOGIN.aspx">GO TO LOGIN FORM</a>
                    </td>
                </tr>
            </table>

            <asp:ValidationSummary ID="ValidationSummary1" runat="server" BackColor="#CCCCCC" Font-Size="Larger" ForeColor="Red" />

        </div>
    </form>
</body>
</html>
Above File is LOGIN_FORM_ASP\REGISTER.aspx








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

namespace LOGIN_FORM_ASP
{
    public partial class REGISTER : System.Web.UI.Page
    {
        string cs = ConfigurationManager.ConnectionStrings["dbcs2"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(cs);
            string query = "insert into signup values(@fname,@lname,@gender,@email,@address,@username,@password)";
            SqlCommand cmd = new SqlCommand(query,con);
            cmd.Parameters.AddWithValue("@fname",FirstNameTextBox.Text);
            cmd.Parameters.AddWithValue("@lname",LastNameTextBox.Text);
            cmd.Parameters.AddWithValue("@gender",DropDownList1.SelectedItem.Value);
            cmd.Parameters.AddWithValue("@email",EmailTextBox.Text);
            cmd.Parameters.AddWithValue("@address",AddressTextBox.Text);
            cmd.Parameters.AddWithValue("@username",UserNameTextBox.Text);
            cmd.Parameters.AddWithValue("@password",PasswordTextBox.Text);
            con.Open();
            int a = cmd.ExecuteNonQuery();
            if (a > 0)
            {
                ClientScript.RegisterStartupScript(typeof(Page), "script", "alert('Signup Successfull.. Username is: " + UserNameTextBox.Text + " and Password is: " + PasswordTextBox.Text + "');",true);
                ClearControls();
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(),"scripts","<script>alert('Signup Failed !!')</script>");
            }
            con.Close();
        }
        void ClearControls()
        {
            FirstNameTextBox.Text = LastNameTextBox.Text = EmailTextBox.Text = AddressTextBox.Text = UserNameTextBox.Text = PasswordTextBox.Text = ConfirmPasswordTextBox.Text = "";
            DropDownList1.ClearSelection();
        }
    }
}
Above File is LOGIN_FORM_ASP\REGISTER.aspx.cs























Comments

Popular posts from this blog

Range Validator Control In ASP.NET Web forms | Form Validation | ASP.NET Web forms

Query String In ASP.NET Web Forms | State Management | ASP.NET | Web Forms

Validation Summary Control In ASP.NET Web forms | Form Validation | ASP.NET