Registration page in Asp.net

Step 1 - Create Tables from below Query

-------------------------------------------------------------------------------------------------------------------

Create Database test

use test

--------------------------------------------------------------------------------------------------------------------

CREATE TABLE tbl_Countries

(
     CountryID INT
    ,CountryName VARCHAR(50)
   
)

INSERT INTO tbl_Countries VALUES (1,'INDIA'),(2,'NEPAL')
//select * from tbl_Countries


CREATE TABLE [dbo].[States](
[StateId] [int] primary key identity,
[CountryID] [int] NOT NULL,
[StateName] [varchar](100) NOT NULL)


INSERT INTO [States] VALUES (1,'UP'),(1,'DELHI')
INSERT INTO [States] VALUES (2,'KATHMANDU'),(2,'POKHRA')
//select * from States




CREATE TABLE [dbo].[Cities](
[CityId] [int] primary key identity,
[StateId] [int] NOT NULL,
[CityName] [varchar](100) NOT NULL
)

INSERT INTO [Cities] VALUES (2,'New Ashok Nagar'),(2,'Saket')
INSERT INTO [Cities] VALUES (1,'GKP'),(1,'MRJ')

//select * from [Cities]

-----------------------------User Table ----------------------------

Create Table tbl_User_Details
(
Id int primary key identity,
FirstName varchar(100),
LastName varchar(100),
EmailId varchar(100),
[Password] varchar(100),
Country int,
State int,
City int
)

----------------------------------------------------------------------------------------------------------------
Step 2 - Right Click on Project Add new Item Select WebForm and Click Ok

    WebForm.aspx Page -

        <!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript" src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
    <link href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript">
        $(function () {
            $("[id*=grd_data_user]").DataTable(
                {
                    bLengthChange: true,
                    lengthMenu: [[5, 10, -1], [5, 10, "All"]],
                    bFilter: true,
                    bSort: true,
                    bPaginate: true
                });
        });
    </script>

</head>
<body>

    <form id="form1" runat="server">
        <table>
            <tr>
                <td>FirstName :</td>
                <td>
                    <asp:TextBox runat="server" ID="txtfirstname" /></td>
            </tr>
            <tr>
                <td>LastName :</td>
                <td>
                    <asp:TextBox runat="server" ID="txtLastName" /></td>
            </tr>

            <tr>
                <td>EmaiId :</td>
                <td>
                    <asp:TextBox runat="server" ID="txtEmail" /></td>
            </tr>
            <tr>
                <td>Password :</td>
                <td>
                    <asp:TextBox runat="server" ID="txtPassword" /></td>
            </tr>


            <tr>
                <td>Country :</td>
                <td>
                    <asp:DropDownList runat="server" Width="100%" ID="ddl_Country" AutoPostBack="true" OnSelectedIndexChanged="ddl_Country_SelectedIndexChanged">
                    </asp:DropDownList>
                </td>
            </tr>


            <tr>
                <td>State :</td>
                <td>
                    <asp:DropDownList runat="server" Width="100%" ID="ddl_State" AutoPostBack="true" OnSelectedIndexChanged="ddl_State_SelectedIndexChanged">
                    </asp:DropDownList>
                </td>
            </tr>

            <tr>
                <td>City :</td>
                <td>
                    <asp:DropDownList runat="server" Width="100%" ID="ddl_City">
                    </asp:DropDownList></td>
            </tr>

            <tr>
                <td></td>
                <td>
                    <asp:Button Text="Submit" runat="server" ID="btn_submit" OnClientClick="return checkvalidation();" OnClick="btn_submit_Click" /></td>
            </tr>

        </table>
        <br />
        <br />
        <table>
                       <tr>
                <td>City :</td>
                <td>
                    <asp:TextBox ID="txtseching" runat="server" placeholder="filter By Name"></asp:TextBox></td>
                           <td>
                    <asp:Button Text="Submit" runat="server" ID="Button1"  OnClick="Button1_Click" /></td>
            </tr>

           

        </table>
        <br />

        <div>
            <asp:GridView ID="grd_data_user" runat="server" EmptyDataText="Record Not Found" AutoGenerateColumns="false" OnRowCommand="grd_data_user_RowCommand">
                <Columns>
                    <asp:TemplateField HeaderText="Sr No">
                        <ItemTemplate>
                            <%#  Container.DataItemIndex +1 %>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="First Name">
                        <ItemTemplate>
                            <%# Eval("FirstName") %>
                        </ItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="Last Name">
                        <ItemTemplate>
                            <%# Eval("LastName") %>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Email">
                        <ItemTemplate>
                            <%# Eval("EmailId") %>
                        </ItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="Password">
                        <ItemTemplate>
                            <%# Eval("Password") %>
                        </ItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="Country">
                        <ItemTemplate>
                            <%# Eval("ctry") %>
                        </ItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="State">
                        <ItemTemplate>
                            <%# Eval("st") %>
                        </ItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="City">
                        <ItemTemplate>
                            <%# Eval("ct") %>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Action">
                        <ItemTemplate>
                            <asp:Button Text="Edit" runat="server" ID="btn_edit" CommandName="Edit_" CommandArgument='<%# Eval("Id") %>' />
                            <asp:Button Text="Delete" runat="server" CommandName="del_" CommandArgument='<%# Eval("Id") %>' ID="btn_delete" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </div>
    </form>

    <script>
        function checkvalidation() {
            var fname = $("#txtfirstname").val();
            var lname = $("#txtLastName").val();
            var email = $("#txtEmail").val();
            var pass = $("#txtPassword").val();
            var Country = $("#ddl_Country").val();
            var State = $("#ddl_State").val();
            var City = $("#ddl_City").val();

            if (fname != "") { }
            else {
                alert('Enter First Name')
                return false;
            }
            if (lname != "") { }
            else {
                alert('Enter Last Name')
                return false;
            }
            var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
            //var address = document.getElementById[email].value;
            if (reg.test(email) == false) {
                alert('Invalid Email Address');
                return (false);
            }
            if (pass != "") { }
            else {
                alert('Enter Password')
                return false;
            }

            if (Country != "Select") { }
            else {
                alert('Select Country')
                return false;
            }

            if (State != "Select") { }
            else {
                alert('Select State')
                return false;
            }

            if (City != "Select") { }
            else {
                alert('Select State')
                return false;
            }

            return true;
        }
    </script>

</body>
</html>
-------------------------------------------------------------------------------------------------------------------------
  WebForm.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.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace CRUDOperation
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Getcounty();
                Getuserdetails();
                ddl_City.Items.Insert(0, "Select");
                ddl_State.Items.Insert(0, "Select");
            }

        }
        public void Getcounty()
        {
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("select CountryID,CountryName from tbl_Countries", con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    ddl_Country.DataSource = dt;
                    ddl_Country.DataValueField = "CountryID";
                    ddl_Country.DataTextField = "CountryName";
                    ddl_Country.DataBind();
                }
                con.Close();
                ddl_Country.Items.Insert(0, "Select");
            }
            catch { }
        }
        protected void ddl_Country_SelectedIndexChanged(object sender, EventArgs e)
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("select StateId,StateName from States where CountryID='" + ddl_Country.SelectedValue + "'", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                ddl_State.DataSource = dt;
                ddl_State.DataValueField = "StateId";
                ddl_State.DataTextField = "StateName";
                ddl_State.DataBind();
            }
            con.Close();
            ddl_State.Items.Insert(0, "Select");
        }
        protected void ddl_State_SelectedIndexChanged(object sender, EventArgs e)
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("select CityId,CityName from [Cities] where StateId='" + ddl_State.SelectedValue + "'", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                ddl_City.DataSource = dt;
                ddl_City.DataValueField = "CityId";
                ddl_City.DataTextField = "CityName";
                ddl_City.DataBind();
            }
            con.Close();
            ddl_City.Items.Insert(0, "Select");
        }
        protected void btn_submit_Click(object sender, EventArgs e)
        {
            if (btn_submit.Text == "Submit")
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("insert into tbl_User_Details(FirstName ,LastName ,EmailId ,Password ,Country ,State ,City ) values(@FirstName ,@LastName ,@EmailId ,@Password ,@Country ,@State ,@City)", con);
                cmd.Parameters.AddWithValue("@FirstName", txtfirstname.Text);
                cmd.Parameters.AddWithValue("@LastName", txtLastName.Text);
                cmd.Parameters.AddWithValue("@EmailId", txtEmail.Text);
                cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
                cmd.Parameters.AddWithValue("@Country", ddl_Country.SelectedValue);
                cmd.Parameters.AddWithValue("@State", ddl_State.SelectedValue);
                cmd.Parameters.AddWithValue("@City", ddl_City.SelectedValue); ;
                cmd.ExecuteNonQuery();
                con.Close();
                ControlClean();
            }
            else if (btn_submit.Text == "Update")
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("update tbl_User_Details set FirstName=@FirstName ,LastName=@LastName ,EmailId=@EmailId ,Password=@Password ,Country=@Country ,State=@State ,City=@City where Id=@Id", con);
                cmd.Parameters.AddWithValue("@FirstName", txtfirstname.Text);
                cmd.Parameters.AddWithValue("@LastName", txtLastName.Text);
                cmd.Parameters.AddWithValue("@EmailId", txtEmail.Text);
                cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
                cmd.Parameters.AddWithValue("@Country", ddl_Country.SelectedValue);
                cmd.Parameters.AddWithValue("@State", ddl_State.SelectedValue);
                cmd.Parameters.AddWithValue("@City", ddl_City.SelectedValue);
                cmd.Parameters.AddWithValue("@Id", ViewState["Id"]);
                cmd.ExecuteNonQuery();
                con.Close();
                btn_submit.Text = "Submit";
                ControlClean();
            }
            Getuserdetails();
        }
        public void Getuserdetails()
        {
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("select *,(select StateName from States where StateId=State) as st,(select CityName from Cities where CityId=City) as ct,(select CountryName from tbl_Countries  where CountryID=Country) as ctry from tbl_User_Details", con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    grd_data_user.DataSource = dt;
                    grd_data_user.DataBind();
                }
                else
                {
                    grd_data_user.DataSource = dt;
                    grd_data_user.DataBind();
                }
                con.Close();
            }
            catch { }
        }
        protected void grd_data_user_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "del_")
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("delete from tbl_User_Details where Id='" + e.CommandArgument + "' ", con);
                cmd.ExecuteNonQuery();
                con.Close();
                Getuserdetails();
            }
            if (e.CommandName == "Edit_")
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("select * from tbl_User_Details where Id='" + e.CommandArgument + "' ", con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    txtfirstname.Text = dt.Rows[0]["FirstName"].ToString();
                    txtLastName.Text = dt.Rows[0]["LastName"].ToString();
                    txtEmail.Text = dt.Rows[0]["EmailId"].ToString();
                    txtPassword.Text = dt.Rows[0]["Password"].ToString();
                    ddl_Country.SelectedValue = dt.Rows[0]["Country"].ToString();
                    ddl_State.SelectedItem.Value = dt.Rows[0]["State"].ToString();
                    ddl_City.SelectedItem.Value = dt.Rows[0]["City"].ToString();
                    btn_submit.Text = "Update";
                    ViewState["Id"] = dt.Rows[0]["Id"].ToString();
                }
                con.Close();
            }
        }
        public void ControlClean()
        {
            txtfirstname.Text = "";
            txtEmail.Text = "";
            txtLastName.Text = "";
            txtPassword.Text = "";
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("select *,(select StateName from States where StateId=State) as st,(select CityName from Cities where CityId=City) as ct,(select CountryName from tbl_Countries  where CountryID=Country) as ctry from tbl_User_Details where  FirstName LIKE '%" + txtseching.Text+ "%'", con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    grd_data_user.DataSource = dt;
                    grd_data_user.DataBind();
                }
                else
                {
                    grd_data_user.DataSource = dt;
                    grd_data_user.DataBind();
                }
                con.Close();
            }
            catch(Exception ex) { }
        }
    }
}
------------------------------------------------------------------------------------------------------------------

Web.config file - 

<connectionStrings>
    <add name="conn" connectionString="Data Source=DESKTOP-48MF2JA;Initial Catalog=test;User ID=sa;Password=sa123"/>
  </connectionStrings>

Comments

Popular posts from this blog

नई दुनिया: COVID-19 के बाद दुनिया अलग कैसे होगी