Dropdown City Connectivity in ASP.net

 

 

Write a program to add city, state and country in the database. Display 3 DropDownLists for city, state and country. Select the country from DropDownList1 then display the state names of that country in the DropDownList2. Select the State from the DropDownList2 and display the city names in the DropDownList3. Select the City name from the DropDownList3 and display the text in the Label like Bhavnagar is the city of Gujarat state which is in the country India. Store the city, state and country names in the database. Retrieve the data of dropdownlist1 first which is country names. As per the selection of the dropdownlist1 retrieve the data of dropdownlist2 which is state names of selected country. As per the selection of the dropdownlist2 retrieve the data from dropdownlist3 which is city names of selected state.

 

 

 Default.Aspx program2 Ass3


Imports System.Data.SqlClient
Imports System.Data
Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            ddlcountry.AppendDataBoundItems = True
            Dim strConnString As [String] = ConfigurationManager.ConnectionStrings("cn").ConnectionString
            Dim strQuery As [String] = "select countryid, countryname from country"

            Dim con As New SqlConnection(strConnString)
            Dim cmd As New SqlCommand()
            cmd.CommandType = CommandType.Text
            cmd.CommandText = strQuery
            cmd.Connection = con

            Try
                con.Open()
                ddlcountry.DataSource = cmd.ExecuteReader()
                ddlcountry.DataTextField = "countryname"
                ddlcountry.DataValueField = "countryid"
                ddlcountry.DataBind()
            Catch ex As Exception
                Throw ex
            Finally
                con.Close()
                con.Dispose()
            End Try
        End If

    End Sub


  
    Protected Sub ddlcountry_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlcountry.SelectedIndexChanged
        ddlstate.Items.Clear()
        ddlstate.Items.Add(New ListItem("--Select State--", ""))
        ddlcity.Items.Clear()
        ddlcity.Items.Add(New ListItem("--Select City--", ""))

        ddlstate.AppendDataBoundItems = True

        Dim strConnString As [String] = ConfigurationManager _
                   .ConnectionStrings("cn").ConnectionString
        Dim strQuery As [String] = "select stateid, statename from state " _
                                         & "where countryid=@countryid"
        Dim con As New SqlConnection(strConnString)
        Dim cmd As New SqlCommand()
        cmd.Parameters.AddWithValue("@countryid", ddlcountry.SelectedItem.Value)
        cmd.CommandType = CommandType.Text
        cmd.CommandText = strQuery
        cmd.Connection = con
        Try
            con.Open()
            ddlstate.DataSource = cmd.ExecuteReader()
            ddlstate.DataTextField = "statename"
            ddlstate.DataValueField = "stateid"
            ddlstate.DataBind()
            If ddlcountry.Items.Count > 1 Then
                ddlcountry.Enabled = True
            Else
                ddlcountry.Enabled = False
                ddlcity.Enabled = False
            End If
        Catch ex As Exception
            Throw ex
        Finally
            con.Close()
            con.Dispose()
        End Try
    End Sub

    Protected Sub ddlcity_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlcity.SelectedIndexChanged
        lbl1.Text = "You Selected : " & _
                    ddlcountry.SelectedItem.Text & " -----> " & _
                     ddlstate.SelectedItem.Text & " -----> " & _
                    ddlcity.SelectedItem.Text
    End Sub

    Protected Sub ddlstate_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlstate.SelectedIndexChanged
        ddlcity.Items.Clear()
        ddlcity.Items.Add(New ListItem("--Select City--", ""))
        ddlcity.AppendDataBoundItems = True
        Dim strConnString As [String] = ConfigurationManager _
                           .ConnectionStrings("cn").ConnectionString
        Dim strQuery As [String] = "select cityid, cityname from city " _
                           & "where stateid=@stateid"
        Dim con As New SqlConnection(strConnString)
        Dim cmd As New SqlCommand()
        cmd.Parameters.AddWithValue("@stateid", _
                                    ddlstate.SelectedItem.Value)
        cmd.CommandType = CommandType.Text
        cmd.CommandText = strQuery
        cmd.Connection = con
        Try
            con.Open()
            ddlcity.DataSource = cmd.ExecuteReader()
            ddlcity.DataTextField = "cityname"
            ddlcity.DataValueField = "cityid"
            ddlcity.DataBind()
            If ddlcity.Items.Count > 1 Then
                ddlcity.Enabled = True
            Else
                ddlcity.Enabled = False
            End If
        Catch ex As Exception
            Throw ex
        Finally
            con.Close()
            con.Dispose()
        End Try
    End Sub
End Class
Dropdown City Connectivity in ASP.net Dropdown City Connectivity in ASP.net Reviewed by Unknown on 3:20 pm Rating: 5

No comments: