Share Blog

Tuesday, May 13, 2014

How to create Registration Form Using Asp.net

SQL DATABASE File

create database student
use student


CREATE TABLE [dbo].[EmployeeData](
      [RegNo] [nvarchar](max) NOT NULL,
      [FName] [nvarchar](50) NOT NULL,
      [LName] [nvarchar](50) NOT NULL,
      [EMailId] [nvarchar](max) NOT NULL,
      [MobileNo] [nvarchar](12) NOT NULL,
      [Gender] [nchar](20) NOT NULL,
      [Address] [nvarchar](max) NOT NULL,
      [Age] [int] NOT NULL,
      [DOB] [datetime] NOT NULL,
      [Location] [nvarchar](max) NOT NULL,
      [State] [nchar](50) NOT NULL,
      Country varchar(80) not null,
      [City] [nchar](50) NOT NULL,
      [Course] [nvarchar](max) NOT NULL,
      [PhotoPath] [nvarchar](80) NOT NULL
)

CREATE TABLE Countries
(
Cntry_ID INT IDENTITY primary key,
Cntry_Name varchar(20) not null
)
CREATE TABLE States
(
State_ID INT IDENTITY primary key,
State_Name varchar(20) not null,
Cntry_ID int references Countries(Cntry_ID)
)


CREATE TABLE Cities
(
Cty_ID INT IDENTITY primary key,
Cty_Name varchar(20) not null,
State_ID INT REFERENCES States(State_ID)
)

insert Countries values('INDIA'),('USA')
SELECT * FROM Countries
INSERT States values('UP',1),('DELHI',1)
INSERT States values('NEW YORK',2),('OREGON',2)
SELECT * FROM States
INSERT Cities values('NOIDA',1),('GAZIABAD',1)
INSERT Cities values('C.P.',2),('CHANDIN CHOWK',2)
INSERT Cities values('ALBANY',3),('LOCK PORT',3)
INSERT Cities values('SALEM',4),('BEND',4)
SELECT * FROM cities



--------------------------------------------------------------------
EMPLOYEE REGISTRATION FORM IN ASP.NET
Design Page:Default.aspx


Default.aspx(Source)

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

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>
<!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">
        .style2
        {
            height: 30px;
        }
        .style3
        {
            height: 26px;
        }
    </style>
    <link type="text/css" rel="Stylesheet" href="StyleSheet.css" />
</head>
<body>
    <form id="form1" runat="server">
    <table class="TABLE" border="1">
        <tr>
            <td colspan="2" style="text-align: center">
                EMPLOYEE REGISTRATION</td>
        </tr>
        <tr>
            <td>
                Enter Your First Name:-</td>
            <td>
                <asp:TextBox ID="TextBox1" runat="server" ValidationGroup="a"></asp:TextBox>
                <ajax:FilteredTextBoxExtender ID="FilteredTextBoxExtender1" runat="server" TargetControlID="TextBox1" FilterMode="InvalidChars" InvalidChars="*&#{}[]()%1234567890"></ajax:FilteredTextBoxExtender>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
                    ControlToValidate="TextBox1" Display="Dynamic"
                    ErrorMessage="FName can't be blank" SetFocusOnError="True"
                    ValidationGroup="a"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td>
                Enter Your Last Name:-</td>
            <td>
                <asp:TextBox ID="TextBox2" runat="server" ValidationGroup="a"></asp:TextBox>
                <ajax:FilteredTextBoxExtender ID="FilteredTextBoxExtender3" runat="server" TargetControlID="TextBox2" FilterMode="InvalidChars" InvalidChars="*&#{}[]()%1234567890"></ajax:FilteredTextBoxExtender>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
                    ControlToValidate="TextBox2" Display="Dynamic"
                    ErrorMessage="LName can't be blank" SetFocusOnError="True"
                    ValidationGroup="a"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td>
                Enter E-mail ID:-</td>
            <td>
                <asp:TextBox ID="TextBox3" runat="server" ValidationGroup="a"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
                    ControlToValidate="TextBox3" Display="Dynamic"
                    ErrorMessage="E-mail Id can't be blank." SetFocusOnError="True"
                    ValidationGroup="a"></asp:RequiredFieldValidator>
                <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
                    ControlToValidate="TextBox3" Display="Dynamic"
                    ErrorMessage="Enter valid e-mail Id" SetFocusOnError="True"
                    ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
                    ValidationGroup="a"></asp:RegularExpressionValidator>
            </td>
        </tr>
        <tr>
            <td>
                Enter Mobile No:-</td>
            <td>
            <ajax:ToolkitScriptManager ID="tksm" runat="server"></ajax:ToolkitScriptManager>

                <asp:TextBox ID="TextBox4" runat="server" ValidationGroup="a" MaxLength="10"></asp:TextBox>
                <ajax:FilteredTextBoxExtender ID="ftb" runat="server" TargetControlID="TextBox4" FilterType="Numbers"></ajax:FilteredTextBoxExtender>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
                    ControlToValidate="TextBox4" Display="Dynamic"
                    ErrorMessage="Mobile No can't be blank" SetFocusOnError="True"
                    ValidationGroup="a"></asp:RequiredFieldValidator>
                <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
                    ControlToValidate="TextBox4" Display="Dynamic"
                    ErrorMessage="Enter valid mobile no." SetFocusOnError="True"
                    ValidationExpression="[7-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]"
                    ValidationGroup="a"></asp:RegularExpressionValidator>
            </td>
        </tr>
        <tr>
            <td>
                Select Gender</td>
            <td>
                <asp:RadioButtonList ID="RadioButtonList1" runat="server"
                    RepeatDirection="Horizontal" ValidationGroup="a">
                    <asp:ListItem>Male</asp:ListItem>
                    <asp:ListItem>Female</asp:ListItem>
                </asp:RadioButtonList>
            </td>
        </tr>
        <tr>
            <td>
                Enter Your Full Address</td>
            <td>
                <asp:TextBox ID="TextBox5" runat="server" Height="54px" TextMode="MultiLine"
                    Width="236px" ValidationGroup="a"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
                    ControlToValidate="TextBox5" Display="Dynamic"
                    ErrorMessage="Address can't be blank" SetFocusOnError="True"
                    ValidationGroup="a"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td>
                Enter Your Age:-</td>
            <td>
                <asp:TextBox ID="TextBox6" runat="server" ValidationGroup="a"></asp:TextBox>
                <ajax:FilteredTextBoxExtender ID="FilteredTextBoxExtender2" runat="server" TargetControlID="TextBox6" FilterType="Numbers"></ajax:FilteredTextBoxExtender>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server"
                    ControlToValidate="TextBox6" Display="Dynamic"
                    ErrorMessage="Age can't be blank" SetFocusOnError="True"
                    ValidationGroup="a"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td>
                Select DOB:-</td>
            <td>
                <asp:TextBox ID="TextBox7" runat="server" ValidationGroup="a"></asp:TextBox>
                <ajax:CalendarExtender ID="cext" runat="server" TargetControlID="TextBox7"></ajax:CalendarExtender>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server"
                    ControlToValidate="TextBox7" Display="Dynamic" ErrorMessage="Select your DOB"
                    SetFocusOnError="True" ValidationGroup="a"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td class="style2">
                Enter Location:-</td>
            <td class="style2">
                <asp:TextBox ID="TextBox8" runat="server" ValidationGroup="a"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server"
                    ControlToValidate="TextBox8" Display="Dynamic"
                    ErrorMessage="Location can't be blank" SetFocusOnError="True"
                    ValidationGroup="a"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td class="style2" colspan="2">
            <asp:UpdatePanel ID="up1" runat="server">
            <ContentTemplate>
            <table style="height: 44px; width: 494px">
            <tr>
            <td>Select Country</td>
            <td>
            <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"  onselectedindexchanged="DropDownList1_SelectedIndexChanged"
                    ValidationGroup="a" CausesValidation="True"></asp:DropDownList>
            </td>
            </tr>
            <tr>
            <td class="style3">
                Select State:-</td>
            <td class="style3">
                <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True"
                    onselectedindexchanged="DropDownList2_SelectedIndexChanged"
                    ValidationGroup="a" CausesValidation="True">
                   

                </asp:DropDownList>
            </td>
            </tr>
         <tr>
            <td>
                Select City:-</td>
            <td>
                <asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="True"
                    ValidationGroup="a">
                </asp:DropDownList>
            </td>
        </tr>
            </table>
            </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="DropDownList1"
                        EventName="SelectedIndexChanged" />
                    <asp:AsyncPostBackTrigger ControlID="DropDownList2"
                        EventName="SelectedIndexChanged" />
                </Triggers>
            </asp:UpdatePanel>
                &nbsp;</td>
        </tr>
       
   
       
        <tr>
            <td>
                Choose Courses done</td>
            <td>
                <asp:CheckBoxList ID="CheckBoxList1" runat="server"
                    RepeatDirection="Horizontal" ValidationGroup="a">
                    <asp:ListItem>MCA</asp:ListItem>
                    <asp:ListItem>MBA</asp:ListItem>
                    <asp:ListItem>BCA</asp:ListItem>
                    <asp:ListItem>B.TECH</asp:ListItem>
                    <asp:ListItem>BBA</asp:ListItem>
                    <asp:ListItem>B.SC.</asp:ListItem>
                    <asp:ListItem>B.COM.</asp:ListItem>
                    <asp:ListItem>B.A.</asp:ListItem>
                </asp:CheckBoxList>
            </td>
        </tr>
        <tr>
            <td>
                Upload photo</td>
            <td>
                <asp:FileUpload ID="FileUpload1" runat="server" />
                <asp:Button ID="Button3" runat="server" onclick="Button3_Click" Text="Upload" />
                <asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server"
                    ControlToValidate="FileUpload1" Display="Dynamic"
                    ErrorMessage="Pls. browse a image" SetFocusOnError="True"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td>
                <asp:Image ID="Image1" runat="server" Height="100" Width="100" />
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td>
                <asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click"
                    style="height: 26px" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <asp:Button ID="Button2" runat="server" Text="Cancel" onclick="Button2_Click" />
            </td>
        </tr>
    </table>
    <div>
   
    </div>
    </form>
</body>
</html>


Default.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.IO;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;


public partial class _Default : System.Web.UI.Page
{
    string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
  //  string file = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlConnection con = new SqlConnection(constring);
            con.Open();
           
            SqlCommand cmd = new SqlCommand("select * from Countries ", con);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                //while (dr.Read())
                //{
                    //ListItem ls = new ListItem(dr[1].ToString(),dr[0].ToString());
                    //DropDownList1.Items.Add(ls);
                    //  DropDownList1.Items.Insert(0, "select");
                    DropDownList1.DataSource = dr;
                    DropDownList1.DataTextField = "Cntry_Name";
                    DropDownList1.DataValueField = "Cntry_ID";
                    DropDownList1.DataBind();
                   
                //}
            }
            con.Close();
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList2.Items.Clear();
        DropDownList3.Items.Clear();
        SqlConnection con = new SqlConnection(constring);
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from States Where Cntry_ID='"+DropDownList1.SelectedValue+"'", con);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows)
        {
            while (dr.Read())
            {
                ListItem ls = new ListItem(dr[1].ToString(), dr[0].ToString());
              //  DropDownList1.Items.Insert(0,"select");
                DropDownList2.Items.Add(ls);
                //DropDownList2.DataTextField = dr["State_Name"].ToString();
                //DropDownList2.DataValueField = dr["State_ID"].ToString();
                //DropDownList2.DataBind();
            }
        }
        con.Close();
       
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
        FileUpload1.SaveAs(Server.MapPath("images/"+filename));
        Image1.ImageUrl = "./images/" + filename;
        Session["file"] = filename;
       // file += filename;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
        FileUpload1.SaveAs(Server.MapPath("images/" + filename));
        string regno = "";
        if (TextBox1.Text.Trim() != "")
        {
            regno += TextBox1.Text.Trim().ToString().Substring(0, 2);
            // regno += DateTime.Now.Day + DateTime.Now.Month + DateTime.Now.Year;
            regno += DateTime.Now.Date.ToShortDateString();
            Random rd = new Random();
            regno += rd.Next();
        }
        else
        {
            Response.Write("<script>alert('FName must be entered')</script>");
        }
        SqlConnection con = new SqlConnection(constring);
        try
        {
       
        con.Open();
    SqlCommand cmd = new SqlCommand("insert into employeedata values(@regno,@fname,@lname,@emailid,@mobileno,@gender,@address,@age,@dob,@location,@country,@state,@city,@course,@photopath)", con);
            cmd.Parameters.AddWithValue("@regno", regno);
            cmd.Parameters.AddWithValue("@fname", TextBox1.Text);
            cmd.Parameters.AddWithValue("@lname", TextBox2.Text);
            cmd.Parameters.AddWithValue("@emailid", TextBox3.Text);
            cmd.Parameters.AddWithValue("@mobileno", TextBox4.Text);
            cmd.Parameters.AddWithValue("@gender", RadioButtonList1.SelectedItem.Text);
            cmd.Parameters.AddWithValue("@address", TextBox5.Text);
            cmd.Parameters.AddWithValue("@age", Convert.ToInt32(TextBox6.Text));
            cmd.Parameters.AddWithValue("@dob", Convert.ToDateTime(TextBox7.Text));
            cmd.Parameters.AddWithValue("@location", TextBox8.Text);
            cmd.Parameters.AddWithValue("@country",DropDownList1.SelectedItem.Text);
            cmd.Parameters.AddWithValue("@state", DropDownList2.SelectedItem.Text);
            cmd.Parameters.AddWithValue("@city", DropDownList3.SelectedItem.Text);
            string course = "";
            if (CheckBoxList1.Items[0].Selected)
            {
                course += CheckBoxList1.Items[0].Text;
            }
            if (CheckBoxList1.Items[1].Selected)
            {
                course += CheckBoxList1.Items[1].Text;
            }
            if (CheckBoxList1.Items[2].Selected)
            {
                course += CheckBoxList1.Items[2].Text;
            }
            if (CheckBoxList1.Items[3].Selected)
            {
                course += CheckBoxList1.Items[3].Text;
            }
            if (CheckBoxList1.Items[4].Selected)
            {
                course += CheckBoxList1.Items[4].Text;
            }
            if (CheckBoxList1.Items[5].Selected)
            {
                course += CheckBoxList1.Items[5].Text;
            }
            if (CheckBoxList1.Items[6].Selected)
            {
                course += CheckBoxList1.Items[6].Text;
            }
            if (CheckBoxList1.Items[7].Selected)
            {
                course += CheckBoxList1.Items[7].Text;
            }


            cmd.Parameters.AddWithValue("@course",course);
            cmd.Parameters.AddWithValue("@photopath",filename);
            int x = cmd.ExecuteNonQuery();
            if (x == 1)
            {
              //  Response.Write("data");
                 Response.Write("<script>alert('"+regno+"')</script>");
                 TextBox1.Text = "";
                 TextBox2.Text = "";
                 TextBox3.Text = "";
                 TextBox4.Text = "";
                 TextBox5.Text = "";
                 TextBox6.Text = "";
                 TextBox7.Text = "";
                 TextBox8.Text = "";
                 RadioButtonList1.SelectedIndex = -1;
                 DropDownList1.SelectedIndex = 0;
                // DropDownList1.Items.Clear();
            }
        }
        catch (SqlException ex)
        {
            Response.Write(ex.Message);
        }
        catch (Exception ex)
        {

            Response.Write(ex.Message);
        }
        finally
        {
            con.Close();
        }

    }
    protected void Button1_Click1(object sender, EventArgs e)
    {

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
        TextBox4.Text = "";
        TextBox5.Text = "";
        TextBox6.Text = "";
        TextBox7.Text = "";
        TextBox8.Text = "";
        RadioButtonList1.SelectedIndex = -1;
        DropDownList1.SelectedIndex = -1;
        DropDownList1.Items.Clear();
    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList3.Items.Clear();
        SqlConnection con = new SqlConnection(constring);
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from Cities Where State_ID='" + DropDownList2.SelectedValue + "'", con);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows)
        {
            while (dr.Read())
            {
                ListItem ls = new ListItem(dr[1].ToString(), dr[0].ToString());
               // DropDownList1.Items.Insert(0, "select");
                DropDownList3.Items.Add(ls);
                //DropDownList3.DataTextField = dr["Cty_Namee"].ToString();
                //DropDownList3.DataValueField = dr["Cty_ID"].ToString();
                //DropDownList3.DataBind();
            }
        }
        con.Close();
    }
}

Web.config
<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
       <system.web>
              <compilation debug="true" targetFramework="4.0"/>
       </system.web>
       <connectionStrings>
              <add name="constr" connectionString="Data Source=admin-pc\sqlexpress;Initial Catalog=student;Integrated Security=True"/>
       </connectionStrings>
</configuration>

No comments:

Post a Comment