Asp.Net Dropdownlist for City State And Country

Introduction  : In this tutorial expalain how to bind dropdownlist like city state and country from the database using asp.net c#. when selecting data from one dropdown it can be fill anothe dropddownlist from the onchange event of dropdownlist.

Step1 Create database  and add table city state country as following

SnapShor : Table Country


 SnapShot : Table State



SnapShot : Table City


Step 3 Html Source

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form2" runat="server">
    <div>
    
        <asp:DropDownList ID="dropCountry" runat="server" Height="33px"  Width="162px" AutoPostBack="True" OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged">
        </asp:DropDownList>
     <br /> <br /> 
        <asp:DropDownList ID="dropState" runat="server" Height="23px" Width="161px" AutoPostBack="True" OnSelectedIndexChanged="ddlState_SelectedIndexChanged"></asp:DropDownList>
        <br /> <br /> 
        <asp:DropDownList ID="dropCity" runat="server" Height="28px" Width="158px" AutoPostBack="True" OnSelectedIndexChanged="ddlCity_SelectedIndexChanged"></asp:DropDownList>
        <br />
    </div>
        <asp:Button ID="Button1" runat="server" Height="33px" OnClick="Button1_Click" Text="Button" Width="74px" />
    </form>
</body>
</html>

Step 4: Code Behind file

 public partial class DDL : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
               //when you run page by
                 Bind_Country();
            }
            
        }
        // Bind Country dropDownList
        protected void Bind_Country()
        {
            DataSet ds = new DataSet();
            SqlDataAdapter adp = new SqlDataAdapter("select * from tbl_Country",con);
            adp.Fill(ds);
            dropCountry.DataSource = ds;
            dropCountry.DataTextField = "CountryName";
            dropCountry.DataValueField = "CountryID";
            dropCountry.DataBind();
            dropCountry.Items.Insert(0,new ListItem("--Select-Country--","0"));
            dropdState.Items.Insert(0, new ListItem("--Select-State--","0"));
            dropdCity.Items.Insert(0, new ListItem("--Select-City--", "0"));
        }

          // Here Bind State dropDownList
        protected void dropdCountry_SelectedIndexChanged(object sender, EventArgs e)
        {
            int CountryId =Convert.ToInt32(dropdCountry.SelectedValue);
            SqlDataAdapter adp = new SqlDataAdapter("select * from tbl_State where CountryID="+CountryId,con);
            DataSet ds = new DataSet();
            adp.Fill(ds);
            dropdState.DataSource = ds;
            dropdState.DataTextField = "StateName"; 
            dropdState.DataValueField = "StateID"; //here you give value of state using StateId
            dropdState.DataBind();
            dropdState.Items.Insert(0,new ListItem("--Select One--","0"));
            con.Close();
        }

        // Here is onchange event of state and bind city 
        protected void dropdState_SelectedIndexChanged(object sender, EventArgs e)
        {
            int StateId = Convert.ToInt32(dropdState.SelectedValue);
            SqlDataAdapter adp = new SqlDataAdapter("select * from tbl_City where StateID=" + StateId, con);
            DataSet ds = new DataSet();
            adp.Fill(ds);
            dropdCity.DataSource = ds;
            dropdCity.DataTextField = "CityName";
            dropdCity.DataValueField = "CityID";   // here you give value of CityName using CityId
            dropdCity.DataBind();
            dropdCity.Items.Insert(0, new ListItem("--Select One--", "0"));
            con.Close();

        }


O/P SnapShot :




Previous
Next Post »

2 comments

Write comments
Unknown
AUTHOR
4 March 2016 at 21:49 delete

nice coding and tutorial

Reply
avatar
Anonymous
AUTHOR
23 May 2017 at 04:31 delete

nice post....

how to make multi column drop down with search in asp.net

Reply
avatar