Related data from one dropdownlist to another in VB.NET

Generally while doing registration in a website. When we choose any city dropdown list, its related states, country and zip code bind in respective dropdownlist.and textboxes.

Default.aspx code

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" 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>Untitled Page</title>
</head>
<
body>
    <form id="form1" runat="server">
    <div>

    </div>
    <asp:Label ID="Label1" runat="server" Text="Choose city" Width="100px"></asp:Label>
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
        <asp:ListItem>select</asp:ListItem>
    </asp:DropDownList>
    <br />
    <asp:Label ID="Label2" runat="server" Text="State" Width="100px"></asp:Label>
    <asp:DropDownList ID="DropDownList2" runat="server">
    </asp:DropDownList>

    <br />
    <asp:Label ID="Label3" runat="server" Text="Country" Width="100px"></asp:Label>
    <asp:DropDownList ID="DropDownList3" runat="server">
    </asp:DropDownList><br />
   <asp:Label ID="Label4" runat="server" Text="Zipcode" Width="100px"></asp:Label>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

    </form>
</body>
</
html>

Default.aspx.vb code

Imports System.Data
Imports System.Data.SqlClient
Partial Class _Default
    Inherits System.Web.UI.Page
    Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ToString()
    Dim con As New SqlConnection(strConnString)
    Dim str As String
    Dim com As SqlCommand
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            con.Open()
            str = "select * from area"
            com = New SqlCommand(str, con)
            Dim reader As SqlDataReader = com.ExecuteReader
            While reader.Read
                DropDownList1.Items.Add(reader("city"))
            End While
            reader.Close()
            con.Close()
        End If
        DropDownList2.Items.Clear()
        DropDownList3.Items.Clear()
        TextBox1.Text = ""
    End Sub

    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
        con.Open()
        str = "select * from area where city='" & DropDownList1.SelectedValue & "'"
        com = New SqlCommand(str, con)
        Dim reader As SqlDataReader = com.ExecuteReader
        Do While reader.Read
            DropDownList2.Items.Add(reader("state"))
            DropDownList3.Items.Add(reader("country"))
            TextBox1.Text = reader("stdcode")
        Loop
        reader.Close()
        con.Close()
    End Sub
End Class

Output
Output-in-windows8.gif


Similar Articles