Sunday, September 13, 2015

Asp.net: Fill Country, State and City dropdownlist

In this article today I am going to explain How to populate the Country, State and City dropdownlist in asp.net

Description:

I want to populate the state dropdown on Country dropdown selection and city dropdown on state dropdown selection. I have created three tables.

Country:
Fill Country, State and City dropdownlist

State:
Fill Country, State and City dropdownlist
City:

Fill Country, State and City dropdownlist

Implementation:
Add a webform to project/website. drag and drop the required control from toolbox to webform.
HTML Markup:
<table>
        <tr>
            <td>Country :</td> <td>State :</td>  <td>City :</td>
        </tr>
           <tr><td></td>
            <td></td> <td></td>
        </tr>
          <tr>
            <td>
                <asp:DropDownList ID="ddlcountry" runat="server" AutoPostBack="true" ></asp:DropDownList></td>
              <td>
                <asp:DropDownList ID="ddlstate" runat="server" AutoPostBack="true"></asp:DropDownList></td>
              <td> <asp:DropDownList ID="ddlcity" runat="server"></asp:DropDownList></td>
        </tr>     
    </table>

Add the namespace
C#:
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

VB:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Bind the Country dropdownlist
Write a function to bind country dropdownlist and call on page load event.
C#:
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString());

protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            FillCountry();
            ddlstate.Items.Insert(0, new ListItem("---Select---", "0"));
            ddlcity.Items.Insert(0, new ListItem("---Select---", "0"));
        }      
    }
    public void FillCountry()
    {
        SqlDataAdapter adp = new SqlDataAdapter("Select * from Tb_Country", con);
        DataTable dt = new DataTable();
        adp.Fill(dt);
        if(dt.Rows.Count>0)
        {
            ddlcountry.DataSource = dt;
            ddlcountry.DataTextField = "CountryName";
            ddlcountry.DataValueField = "ID";
            ddlcountry.DataBind();
            ddlcountry.Items.Insert(0, new ListItem("---Select---", "0"));
        }
    }

VB:
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("connection").ToString())

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            FillCountry()
            ddlstate.Items.Insert(0, New ListItem("---Select---", "0"))
            ddlcity.Items.Insert(0, New ListItem("---Select---", "0"))
        End If
    End Sub
    Public Sub FillCountry()
        Dim adp As New SqlDataAdapter("Select * from Tb_Country", con)
        Dim dt As New DataTable()
        adp.Fill(dt)
        If dt.Rows.Count > 0 Then
            ddlcountry.DataSource = dt
            ddlcountry.DataTextField = "CountryName"
            ddlcountry.DataValueField = "ID"
            ddlcountry.DataBind()
            ddlcountry.Items.Insert(0, New ListItem("---Select---", "0"))
        End If
    End Sub

Populate the State Dropdownlist
To populate the state dropdownlist write the below given code on SelectedIndexChnaged event of Country dropdown
C#:
protected void ddlcountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlDataAdapter adp = new SqlDataAdapter("Select * from Tb_State where CountryId= @id", con);
        adp.SelectCommand.Parameters.AddWithValue("id",ddlcountry.SelectedValue);
        DataTable dt = new DataTable();
        adp.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            ddlstate.DataSource = dt;
            ddlstate.DataTextField = "StateName";
            ddlstate.DataValueField = "Stateid";
            ddlstate.DataBind();
            ddlstate.Items.Insert(0, new ListItem("---Select---", "0"));
        }
        else
        {
            ddlcity.Items.Clear();
            ddlcity.Items.Insert(0, new ListItem("---Select---", "0"));
            ddlstate.Items.Clear();
            ddlstate.Items.Insert(0, new ListItem("---Select---", "0"));
        }
    }

VB:
   Protected Sub ddlcountry_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlcountry.SelectedIndexChanged
        Dim adp As New SqlDataAdapter("Select * from Tb_State where CountryId= @id", con)
        adp.SelectCommand.Parameters.AddWithValue("id", ddlcountry.SelectedValue)
        Dim dt As New DataTable()
        adp.Fill(dt)
        If dt.Rows.Count > 0 Then
            ddlstate.DataSource = dt
            ddlstate.DataTextField = "StateName"
            ddlstate.DataValueField = "Stateid"
            ddlstate.DataBind()
            ddlstate.Items.Insert(0, New ListItem("---Select---", "0"))
        Else
            ddlcity.Items.Clear()
            ddlcity.Items.Insert(0, New ListItem("---Select---", "0"))
            ddlstate.Items.Clear()
            ddlstate.Items.Insert(0, New ListItem("---Select---", "0"))
        End If
    End Sub

Populate city dropdownlist
Write the below given code on State dropdownlist SelectedIndexChanged event
C#:
protected void ddlstate_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlDataAdapter adp = new SqlDataAdapter("Select * from Tb_City where StateId= @id", con);
        adp.SelectCommand.Parameters.AddWithValue("id", ddlstate.SelectedValue);
        DataTable dt = new DataTable();
        adp.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            ddlcity.DataSource = dt;
            ddlcity.DataTextField = "CityName";
            ddlcity.DataValueField = "CityId";
            ddlcity.DataBind();
            ddlcity.Items.Insert(0, new ListItem("---Select---", "0"));
        }
        else
        {
            ddlcity.Items.Clear();
            ddlcity.Items.Insert(0, new ListItem("---Select---", "0"));
        }
    }

VB:
Protected Sub ddlstate_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlstate.SelectedIndexChanged
        Dim adp As New SqlDataAdapter("Select * from Tb_City where StateId= @id", con)
        adp.SelectCommand.Parameters.AddWithValue("id", ddlstate.SelectedValue)
        Dim dt As New DataTable()
        adp.Fill(dt)
        If dt.Rows.Count > 0 Then
            ddlcity.DataSource = dt
            ddlcity.DataTextField = "CityName"
            ddlcity.DataValueField = "CityId"
            ddlcity.DataBind()
            ddlcity.Items.Insert(0, New ListItem("---Select---", "0"))
        Else
            ddlcity.Items.Clear()
            ddlcity.Items.Insert(0, New ListItem("---Select---", "0"))
        End If
    End Sub

Build and run the project.
 Result:
Fill Country, State and City dropdownlist
  In this article we have learn to how to populate Country, State and City dropdownlist  on each other selection in asp.netI hope you enjoyed this article. 

3 comments:

  1. i like this article very much. Thanks a lot and keep updating. Right now i am working on a project can you help me on timely basis as and when i need your help.

    Thanks n Regards,
    Vineet Goyal

    ReplyDelete