.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Post New Resource Bookmark and Share   

Show Related data from one dropdownlist to another

Posted By:satyapriyanayak       Posted Date: April 16, 2013    Points: 200    Category:    URL: http://www.dotnetspark.com  

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.
 

Program
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


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend