ARTICLE

How to bind country, state and city with DropDownList in ASP. NET using VB.NET

Posted by Rohatash Kumar Articles | ASP.NET using VB.NET June 28, 2011
This article displays countries, states and cities with DropDownList in ASP.NET.
Download Files:
 
Reader Level:

This article shows how to create three DropDownList to display countries, states and cities. When you select a country then its automatically shows the states name of that country in next DropDownList. Then when you select a state, the cities DropDownList will fetch the related cities for that state.

Creating table for country, state and city.

Country Table

Create Table Country

(

CountryId Int Primary Key,

County Varchar(30)

)

Countrystate Table

Create Table countryState

(

StateId Int Primary Key,

CountryId Int Foreign Key References Country(CountryId),

State Varchar(30)

)

StateCity Table

Create Table stateCity

(

CityId Int,

StateId Int Foreign Key References countryState(StateId),

City Varchar(30)

)

Now insert values in the table.

Insert Into Country Values(101,'India')

Insert Into Country Values(102,'USA')

Insert Into Country Values(103,'Pakistan')

 

Insert Into countryState Values(1001,101,'U.P')

Insert Into countryState Values(1002,101,'Kerala')

Insert Into countryState Values(1003,101,'Kasmir')

Insert Into countryState Values(2001,102,'Colorado')

Insert Into countryState Values(2002,102,'Delaware')

Insert Into countryState Values(2003,102,'Georgia')

Insert Into countryState Values(3001,103,'Punjap')

Insert Into countryState Values(3002,103,'Baluchistan')

Insert Into countryState Values(3003,103,'Sind')

 

Insert Into stateCity Values(11,1001,'Kanpur')

Insert Into stateCity Values(12,1001,'Dg')

Insert Into stateCity Values(21,1002,'Pal')

Insert Into stateCity Values(22,1002,'Tri')

Insert Into stateCity Values(31,1003,'Jammu')

Insert Into stateCity Values(32,1003,'Manali')

Insert Into stateCity Values(41,2001,'Alabama')

Insert Into stateCity Values(42,2001,'Arizona')

Insert Into stateCity Values(51,2002,'Bellefonte')

Insert Into stateCity Values(52,2002,'Felton')

Insert Into stateCity Values(61,2003,'Rustavi')

Insert Into stateCity Values(62,2003,'Kobulati')

Insert Into stateCity Values(71,3001,'Lahore')

Insert Into stateCity Values(72,3001,'Faisalabad')

Insert Into stateCity Values(81,3002,'Quetta')

Insert Into stateCity Values(82,3002,'Nasirabad')

Insert Into stateCity Values(91,3003,'Krachi')

Insert Into stateCity Values(92,3003,'Mirpur khas')

Now select it.

select * from Country;

select * from  countryState;

select * from stateCity;

Now drag and drop three DropDownList to display countries, states and cities and three update panel control on the page.

ddl1.gif

Figure1

.aspx code

<head runat="server">

   <title></title>

   <style type="text/css">

       .style1

        {

           color: #000066;

        }

   </style>

</head>

<body>

   <form id="form1" runat="server">

  <asp:ScriptManager ID="ScriptManager1" runat="server">

   </asp:ScriptManager>

   <div>

       <asp:UpdatePanel ID="countrypanel" runat="server">

          <ContentTemplate >

              <span class="style1"><strong>Select Country:</strong></span>

             <asp:DropDownList ID="ddlcountry" AutoPostBack ="true" AppendDataBoundItems

="true" runat="server" Height="20px" Width="156px"   

onselectedindexchanged="ddlcountry_SelectedIndexChanged" BackColor="#3399FF"

                  ForeColor="#FF9999">

           </asp:DropDownList>

       </ContentTemplate>

 

     <Triggers>

      <asp:AsyncPostBackTrigger ControlID ="ddlcountry" />

     </Triggers>

  </asp:UpdatePanel>

       <br />

 

 <asp:UpdatePanel ID="statepanel" runat="server">    

    <ContentTemplate >

        <span class="style1"><strong>&nbsp;&nbsp;&nbsp;&nbsp; Select State:</strong></span>

      <asp:DropDownList ID="ddlstate" AutoPostBack ="true"

       AppendDataBoundItems ="true"  runat="server" Height="20px"

Width="155px"               onselectedindexchanged="ddlstate_SelectedIndexChanged"

            BackColor="#FF3399" ForeColor="Maroon">

      </asp:DropDownList>

    </ContentTemplate>

    <Triggers >

       <asp:AsyncPostBackTrigger ControlID ="ddlstate" />

       </Triggers>

    </asp:UpdatePanel>

       <br />

 

<asp:UpdatePanel ID="citypanel" runat="server">     

   <ContentTemplate >      

       <span class="style1"><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Select City:</strong></span>      

     <asp:DropDownList ID="ddlcity" AutoPostBack ="true"

   AppendDataBoundItems ="true" runat="server" Height="20px" Width="155px"

           BackColor="#66FFFF" ForeColor="#006666">

     </asp:DropDownList>

  </ContentTemplate>

  <Triggers >

    <asp:AsyncPostBackTrigger ControlID ="ddlcity" />         </Triggers>

      

 </asp:UpdatePanel>

 </div>

   </form>

</body>

</html>

 

.VB code

 

Imports System.Data.SqlClient

Imports System.Data

 

Public Class WebForm1

   Inherits System.Web.UI.Page

   Private conn As New SqlConnection("Data source=.; uid=sa; pwd=Password$2; database=master")

 

   Public Sub Bind_ddlCountry()

        conn.Open()

       Dim cmd As New SqlCommand("select County,CountryId from Country", conn)

       Dim dr As SqlDataReader = cmd.ExecuteReader()

        ddlcountry.DataSource = dr

        ddlcountry.Items.Clear()

        ddlcountry.Items.Add("--Please Select country--")

        ddlcountry.DataTextField = "County"

        ddlcountry.DataValueField = "CountryId"

        ddlcountry.DataBind()

        conn.Close()

   End Sub

 

   Public Sub Bind_ddlState()

        conn.Open()

       Dim cmd As New SqlCommand("select State,StateID from countryState where CountryId='" + ddlcountry.SelectedValue &"'", conn)

       Dim dr As SqlDataReader = cmd.ExecuteReader()

        ddlstate.DataSource = dr

        ddlstate.Items.Clear()

        ddlstate.Items.Add("--Please Select state--")

        ddlstate.DataTextField = "State"

        ddlstate.DataValueField = "StateID"

        ddlstate.DataBind()

        conn.Close()

 

   End Sub

   Public Sub Bind_ddlCity()

        conn.Open()

       Dim cmd As New SqlCommand("select * from stateCity where StateId ='" + ddlstate.SelectedValue &"'", conn)

       Dim dr As SqlDataReader = cmd.ExecuteReader()

        ddlcity.DataSource = dr

        ddlcity.Items.Clear()

        ddlcity.Items.Add("--Please Select city--")

        ddlcity.DataTextField = "City"

        ddlcity.DataValueField = "CityID"

        ddlcity.DataBind()

        conn.Close()

   End Sub

   Protected Sub Page_Load(ByVal senderAs Object,ByVal e As EventArgs) Handles Me.Load

       If Not IsPostBack Then

            Bind_ddlCountry()

       End If

   End Sub

 

   Protected Sub ddlcountry_SelectedIndexChanged(ByVal sender As Object, ByVal eAs EventArgs)Handles ddlcountry.SelectedIndexChanged

        Bind_ddlState()

   End Sub

 

   Protected Sub ddlstate_SelectedIndexChanged(ByVal sender As Object, ByVal eAs EventArgs)Handles ddlstate.SelectedIndexChanged

        Bind_ddlCity()

   End Sub

End Class

 

 

Now run the application and test it.


ddl2.gif

 

Figure2

 

Now select a country, then its automatically shows the states name of that country in next DropDownList. For example we select Pakistan.


ddl3.gif

 

Figure3

 

Now select a State, then its automatically shows the cities name of that state in next DropDownList. For example we select Punjab.


ddl4.gif

 

Figure4


Note: You can see a demo of this article by downloading this application.

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Diagram
Become a Sponsor