ASP 101 - Active Server Pages 101 db_pulldown_linked.aspx
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script language="VB" runat="server">
	Sub Page_Load(sender as Object, e as EventArgs)
		' Only pull data for the first list from db on first page run.
		If Not Page.IsPostBack Then
			Dim myConnection As SqlConnection
			Dim myCommand    As SqlCommand
			Dim myDataReader As SqlDataReader
			Dim strSQLQuery  As String
			' Create connection and set connection string	
			myConnection = New SqlConnection("Data Source=10.2.2.133;" _
	& "Initial Catalog=samples;User Id=samples;Password=password;" _
	& "Connect Timeout=15;Network Library=dbmssocn;")
			' Build our SQL query
			strSQLQuery = "SELECT id, " _
				& "RTRIM(first_name) + ' ' + RTRIM(last_name) AS name " _
				& "FROM sample ORDER BY id;"
			' Create a new command object that uses our connection
			' and set the text of the command to be executed
			myCommand = New SqlCommand(strSQLQuery, myConnection)
			myConnection.Open()
			' Get a new datareader from our command
			myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
			' We've got our data... now connect it to our list:
			ddlNamesList.DataSource = myDataReader
			ddlNamesList.DataValueField = "id"
			ddlNamesList.DataTextField = "name"
			ddlNamesList.DataBind()
			' Hide the second DDL since it'll be blank until the form is submitted.
			ddlSalesList.Visible = False
		End If
	End Sub
	Sub SubmitBtn_Click(sender as Object, e as EventArgs)
		lblSelection.Text = "You selected the item with id #" _
			& ddlNamesList.SelectedItem.Value _
			& " which corresponds to the name " _
			& ddlNamesList.SelectedItem.Text _
			& ".  The second DropDownList contains a list " _
			& "of the fictitious sales this fictitious " _
			& "salesman has made."
			' Get data to fill second DropDownList
			Dim myConnection As SqlConnection
			Dim myCommand    As SqlCommand
			Dim myDataReader As SqlDataReader
			Dim strSQLQuery  As String
			' Create connection and set connection string	
		myConnection = New SqlConnection("Data Source=10.2.2.133;" _
	& "Initial Catalog=samples;User Id=samples;Password=password;" _
	& "Connect Timeout=15;Network Library=dbmssocn;")
			' Build our SQL query.  We pull in the salesman's id from the
			' item selected from the first DropDownList.
			strSQLQuery = "SELECT Timestamp, " _
				& "'$' + CONVERT(varchar(10), Amount) + ' on ' + CONVERT(varchar(25), Timestamp) AS SalesDetails " _
				& "FROM sales " _
				& "WHERE SalesmanId = " & ddlNamesList.SelectedItem.Value _
				& "ORDER BY timestamp;"
			' Create our command object
			myCommand = New SqlCommand(strSQLQuery, myConnection)
			myConnection.Open()
			' Get a new datareader from our command
			myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
			' Connect data to the sales data list:
			ddlSalesList.DataSource = myDataReader
			ddlSalesList.DataValueField = "Timestamp"
			ddlSalesList.DataTextField = "SalesDetails"
			ddlSalesList.DataBind()
			' Make the second list visible now that its got some data in it.
			ddlSalesList.Visible = True
	End Sub
</script>
<html>
<head>
<title>ASP.NET Linked Database Dropdown Lists Sample</title>
</head>
<body>
<form runat=server>
<asp:DropDownList id="ddlNamesList" runat="server" />
<asp:DropDownList id="ddlSalesList" runat="server" />
<asp:Button id="btnSubmit" text="Submit"
	OnClick="SubmitBtn_Click" runat="server" />
</form>
<p>
<asp:Label id="lblSelection" runat="server" />
</p>
<hr />
<p>
<strong>Note:</strong>  There is no additional data to be displayed once
you select a sale from the second DropDownList so submitting the form again
after selecting a sale doesn't do anything aside from reloading the data.
That being said... there's no reason it couldn't do something (like display
the full details of a sale) and in most cases you'd probably want it to.
</p>
<hr />
<p>
Click <a href="http://www.asp101.com/samples/db_pulldown_linked_aspx.asp">here</a> to read about and download the source code.
</p>
</body>
</html>