Share Blog

Friday, May 09, 2014

How to Bind Country And State and City in Asp.net with Sql server

---------Sql  server Table--------------
create database adonet
use adonet
create table country(countryid int primary key,country varchar(100))
create table state (stateid int primary key,
countryid int foreign key references country(countryid),state varchar(100))
create table city(cityid int primary key,
stateid int foreign key references state(stateid),city varchar(100))
insert into country values(1,'India'),(2,'Pakistan'),(3,'US')
select * from country

insert into state  values(11,1,'MP'),(12,1,'UP'),(13,1,'Hariyada'),(21,2,'punjab'),
(22,2,'sind'),(23,2,'baluchistan'),(31,3,'Georgia'),(32,3,'Colorado')
select * from state

insert into city  values(111,11,'Jhasi'),(112,11,'Bhopal'),(113,11,'Indor'),
(211,12,'Noida'),(212,12,'kanpur'),
(311,13,'Gudgaon'),(312,13,'Hisar'),
(411,21,'Lahor'),(412,21,'Faislabad'),
(511,22,'Karachi'),(512,22,'Mirpur Khas'),
(611,23,'nasirabad'),(612,23,'quetta'),
(711,31,'rustavi'),(712,31,'kobulati'),
(811,32,'arizona'),(812,32,'Alabama')
select * from city
-----------RegistrationPage-----
<%@ 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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        Counrty Name&nbsp;
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
            onselectedindexchanged="DropDownList1_SelectedIndexChanged">
        </asp:DropDownList>
        <br />
        State Name
        <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True"
            onselectedindexchanged="DropDownList2_SelectedIndexChanged">
        </asp:DropDownList>
        <br />
        City Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="True">
        </asp:DropDownList>
   
    </div>
    </form>
</body>
</html>

------for Registrationpage.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;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=SUNIL;Initial Catalog=adonetpractices;Integrated Security=True");
    public void BindCountry()
    {
        SqlCommand cmd = new SqlCommand("select country,countryid from country",con);
        con.Open();
        SqlDataReader dr =cmd.ExecuteReader();
        DropDownList1.DataSource = dr;
        DropDownList1.Items.Clear();
        //DropDownList1.Items.Add("-- Please select Country");
        DropDownList1.DataTextField = "country";
        DropDownList1.DataValueField = "countryid";
        DropDownList1.DataBind();
        DropDownList1.Items.Insert(0,new ListItem("select Country","0"));
        DropDownList2.Items.Insert(0,new ListItem("select State","0"));
        DropDownList3.Items.Insert(0, new ListItem("select city", "0"));
        con.Close();
    }
    public void bindstate()
    {
        SqlCommand cmd = new SqlCommand("select state,stateid from state where countryid= '"+DropDownList1.SelectedValue+"' ", con);
        con.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        DropDownList2.DataSource = dr;
        DropDownList2.Items.Clear();
        DropDownList2.Items.Add("-- Please select state");
        DropDownList2.DataTextField = "state";
        DropDownList2.DataValueField = "stateid";
        DropDownList2.DataBind();
        con.Close();
    }
    public void bindcity()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from city where stateid='"+DropDownList2.SelectedValue+"'",con);
        SqlDataReader dr = cmd.ExecuteReader();
        DropDownList3.DataSource = dr;
        DropDownList3.Items.Clear();
        DropDownList3.Items.Add("--please select city");
        DropDownList3.DataTextField = "city";
        DropDownList3.DataValueField = "cityid";
        DropDownList3.DataBind();
        con.Close();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindCountry();
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindstate();
    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindcity();
    }
}
-------result-----








No comments:

Post a Comment