Wednesday 5 June 2013

SIMPLE REGISTRATION FORM IN ASP.NET


SIMPLE REGISTRATION FORM IN ASP.NET WITH C#


In this article we will see a simple registration form in asp.net with all client side validations. I am designing a registration form with fields like first name, last name, email, etc,.. and inserting the data into database using insert sql statement . Here i am adding a local database to my asp,net application and using the local database. The connection string is taken in web.config file . you can download the source code from the link below .
Create table in sql server
Registratio_Table








I am designing my aspx page as follows :
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UserRegistration.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">
        .style4
        {
            width: 212px;
        }
        .style7
        {
            width: 212px;
            height: 31px;
        }
        .style9
        {
            height: 26px;
        }
        .style11
        {
            width: 259px;
        }
        .style12
        {
            width: 259px;
            height: 31px;
        }
    </style>
</head>
<body>
    <formid="form1" runat="server">
    <div>
        <tablestyle="border:1ptsolid#6666FF;width:60%;height:424px;font-family: Verdana;
            border-collapse:collapse;background-color:#ffffff;"align="center">
            <tr>
                <tdalign="center"colspan="3"class="style9">
                    <asp:LabelID="lblHeader" runat="server"Text="Registration Form" Font-Bold="True"></asp:Label>
                </td>
            </tr>
            <tr>
                <tdclass="style11"align="right">
                    <asp:LabelID="lblFirstName" runat="server"Text="First Name :"></asp:Label>
                </td>
                <tdclass="style11"align="left">
                    <asp:TextBoxID="txtFirstName" runat="server"></asp:TextBox>
                </td>
                <tdclass="style4"align="left">
                    <asp:RequiredFieldValidatorID="RfvFirstName" runat="server" ErrorMessage="* Required"
                        ForeColor="#FF3300" ControlToValidate="txtFirstName"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <tdclass="style11"align="right">
                    <asp:LabelID="lblLastName" runat="server"Text="Last Name :"></asp:Label>
                </td>
                <tdclass="style11"align="left">
                    <asp:TextBoxID="txtLastName" runat="server"></asp:TextBox>
                </td>
                <tdclass="style4"align="left">
                    <asp:RequiredFieldValidatorID="RfvLastName" runat="server" ErrorMessage="* Required"
                        ForeColor="#FF3300" ControlToValidate="txtLastName"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <tdclass="style11"align="right">
                    <asp:LabelID="lblEmail" runat="server"Text="E-Mail :"></asp:Label>
                </td>
                <tdclass="style11"align="left">
                    <asp:TextBoxID="txtEmail" runat="server"></asp:TextBox>
                </td>
                <tdclass="style4"align="left">
                    <asp:RequiredFieldValidatorID="RfvUserName" runat="server" ErrorMessage="* Required"
                        ForeColor="#FF3300" ControlToValidate="txtEmail"></asp:RequiredFieldValidator>
                    <br/>
                    <asp:RegularExpressionValidatorID="RxvEmail" runat="server"
                        ControlToValidate="txtEmail" ErrorMessage="Invalid E-mail" ForeColor="#FF3300"
                        ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
                </td>
            </tr>
            <tr>
                <tdclass="style11"align="right">
                    <asp:LabelID="lblPassword" runat="server"Text="Password :"></asp:Label>
                </td>
                <tdclass="style11"align="left">
                    <asp:TextBoxID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
                </td>
                <tdclass="style4"align="left">
                    <asp:RequiredFieldValidatorID="RfvPwd" runat="server" ErrorMessage="* Required"
                        ForeColor="#FF3300" ControlToValidate="txtPassword"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <tdclass="style11"align="right">
                    <asp:LabelID="lblConfirmPwd" runat="server"Text="Confirm Pasword :"></asp:Label>
                </td>
                <tdclass="style11"align="left">
                    <asp:TextBoxID="txtConfirmPwd" runat="server" TextMode="Password"></asp:TextBox>
                </td>
                <tdclass="style4"align="left">
                    &nbsp;
                    <asp:RequiredFieldValidatorID="RfvCnfrmPwd" runat="server" ErrorMessage="* Required"
                        ForeColor="#FF3300" ControlToValidate="txtConfirmPwd"></asp:RequiredFieldValidator>
                    <br/>
                    <asp:CompareValidatorID="CvCnfmPwd" runat="server" ErrorMessage="Password and Confirm Password didnt matched"
                        ForeColor="#FF3300" ControlToCompare="txtPassword"
                        ControlToValidate="txtConfirmPwd"></asp:CompareValidator>
                </td>
            </tr>
            <tr>
                <tdclass="style11"align="right">
                    <asp:LabelID="lblGender" runat="server"Text="Gender :"></asp:Label>
                </td>
                <tdclass="style11"align="left">
                    <asp:RadioButtonListID="RdoGender" runat="server" RepeatDirection="Horizontal"
                        Width="176px">
                        <asp:ListItem>Male</asp:ListItem>
                        <asp:ListItem>Female</asp:ListItem>
                    </asp:RadioButtonList>
                </td>
                <tdclass="style4"align="left">
                    <asp:RequiredFieldValidatorID="RfvGender" runat="server" ErrorMessage="* Required"
                        ForeColor="#FF3300" ControlToValidate="RdoGender"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <tdclass="style11"align="right">
                    <asp:LabelID="lblDob" runat="server"Text="Date of Birth :"></asp:Label>
                </td>
                <tdclass="style11"align="left">
                    <asp:TextBoxID="txtDob" runat="server"></asp:TextBox>
                    &nbsp;<asp:LabelID="Label1" runat="server"Text="(dd/mm/yyyy)"></asp:Label>
                </td>
                <tdclass="style4"align="left">
                    <asp:RequiredFieldValidatorID="RequiredFieldValidator5" runat="server" ErrorMessage="* Required"
                        ForeColor="#FF3300" ControlToValidate="txtDob"></asp:RequiredFieldValidator>
                    &nbsp;<br/>
                    <asp:RegularExpressionValidatorID="RxvDob" runat="server" ControlToValidate="txtDob"
                        ErrorMessage="Invalid Date Format"
                        ValidationExpression="^(((((0[1-9])|(1\d)|(2[0-8]))\/((0[1-9])|(1[0-2])))|((31\/((0[13578])|(1[02])))|((29|30)\/((0[1,3-9])|(1[0-2])))))\/((20[0-9][0-9])|(19[0-9][0-9])))|((29\/02\/(19|20)(([02468][048])|([13579][26]))))$"
                        ForeColor="#FF3300"></asp:RegularExpressionValidator>
                </td>
            </tr>
            <tr>
            <tdalign="right"class="style11">
                <asp:LabelID="lblMobile" runat="server"Text="Mobile"></asp:Label>
                </td>
            <tdclass="style11">

                <asp:TextBoxID="txtMobile" runat="server"MaxLength="10"></asp:TextBox>

                </td>
            <tdclass="style4">
                <asp:RegularExpressionValidatorID="RxvMobile" runat="server"
                    ErrorMessage="Invalid Mobile Number" ForeColor="#FF3300"
                    ValidationExpression="^([7-9]{1})([0-9]{9})$"
                    ControlToValidate="txtMobile"></asp:RegularExpressionValidator>
                </td>
            </tr>
            <tr>
                <tdclass="style11"align="right">
                    <asp:LabelID="lblAddress" runat="server"Text="Address :"></asp:Label>
                </td>
                <tdclass="style11"align="left">
                    <asp:TextBoxID="txtAddress" runat="server" TextMode="MultiLine"></asp:TextBox>
                </td>
                <tdclass="style4"align="left">
                    <asp:RequiredFieldValidatorID="RfvAddress" runat="server" ErrorMessage="* Required"
                        ForeColor="Red" ControlToValidate="txtAddress"></asp:RequiredFieldValidator>
                </td>
            </tr>

                <tdalign="center"class="style12">
                    &nbsp;
                    <asp:LabelID="lblMsg" runat="server" ForeColor="#CC3300"></asp:Label>
                </td>
                <tdclass="style12">
                    <asp:ButtonID="btnSubmit" runat="server"Text="Submit"OnClick="btnSubmit_Click"/>&nbsp;
                    <asp:ButtonID="btnClear" runat="server" CausesValidation="False"OnClick="btnClear_Click"
                        Text="Clear"/>
                </td>
                <tdalign="center"class="style7">
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>
The c# code is like this ..
using System;
using System.Web.UI;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    //declaring connection string and command
    //here we are extracting connection string from web.config file
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnection"].ToString());
    SqlCommand cmd = new SqlCommand();

    protected void btnSubmit_Click(object sender, EventArgs e)
    {    
     try
     {   
        cmd.Connection = con; //assigning connection to command
        cmd.CommandType = CommandType.Text; //representing type of command
        //cmd.CommandText = "INSERT INTO UserDetails (Fname,Lname,Email,Password,Gender,Dob,Mobile,Address) values
        // (@Fname,@Lname,@Email,@Password,@Gender,@Dob,@Mobile,@Address)";
        cmd.CommandText = "INSERT INTO UserDetails values(@Fname,@Lname,@Email,@Password,@Gender,@Dob,@Mobile,@Address)";

        //adding parameters with value
        cmd.Parameters.AddWithValue("@Fname", txtFirstName.Text.ToString());
        cmd.Parameters.AddWithValue("@Lname", txtLastName.Text.ToString());
        cmd.Parameters.AddWithValue("@Email",txtEmail.Text.ToString());
        cmd.Parameters.AddWithValue("@Password",txtPassword.Text.ToString());
        cmd.Parameters.AddWithValue("@Gender",RdoGender.SelectedItem.Text.ToString());
        cmd.Parameters.AddWithValue("@Dob",txtDob.Text.ToString());
        cmd.Parameters.AddWithValue("@Mobile",txtMobile.Text.ToString());
        cmd.Parameters.AddWithValue("@Address",txtAddress.Text.ToString());
        con.Open(); //opening connection
        cmd.ExecuteNonQuery();  //executing query
        con.Close(); //closing connection
        lblMsg.Text="Registered Successfully..";

     }
     catch(Exception ex)
     {
        lblMsg.Text=ex.Message.ToString();
     }
    }

    protected void btnClear_Click(object sender, EventArgs e)
    {
        //refreshing/reloading page to clear all the controls
        Page.Response.Redirect(Page.Request.Url.ToString(), true);
    }
}
..Registration_Image_1Download Source Code

No comments:

Post a Comment