Relational DropDownList Using VB.NET

Relational DropDownList Using VB.NET Code Example. In this code, we illustrate three steps:
Step 1. Copy and paste the code below on to the newly created aspx file
Step 2. Add <appSettings> tag to the existing web.config
Step 3. Make sure you have Northwind database install in your system

Contributed by
Rating: 4 stars4 stars4 stars4 stars4 stars / 120
July 16, 2003
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement
******************************

 <!-- ASPX Page --><!-- Step 1. Copy and paste the code below on to the newly created aspx file --><!-- Step 2. Add <appSettingstag to the existing web.config --><!-- Step 3. Make sure you have Northwind database install in your system --><%@ Import Namespace="System.Data" %><%@ Import Namespace="System.Data.SqlClient" %>
<
SCRIPT language=VB runat="server">

Public 
Sub Page_Load(Source As ObjectAs EventArgs)

If 
Not IsPostBack Then

Dim _Conn 
As SqlConnection
Dim _Command 
As SqlCommand
Dim _Reader 
As SqlDataReader
Dim SQL 
As String

SQL 
"select * from Region"

_Conn GetConnection()

_Command = new SqlCommand(SQL_Conn)

_Reader _Command.ExecuteReader()

Regions.Items.Add(new ListItem("Select Region...""0")) 

While 
_Reader.Read()
Regions.Items.Add(new ListItem(_Reader("RegionDescription").ToString(), _Reader("RegionID").ToString()))
End While

Territory.Items.Add(new ListItem("Select Territory...""0"))

End If

End Sub

Function GetConnection() As SqlConnection
Dim _Connection 
As SqlConnection
Dim ConnStr 
As String

ConnStr 
ConfigurationSettings.AppSettings("ConnectionString")

_Connection = new SqlConnection(ConnStr)
_Connection.Open()

Return 
_Connection
End 
Function 


Sub OnSelectedRegionIndexChanged(Source As ObjectAs EventArgs)
Dim _Region As Integer

_Region 
Regions.SelectedItem.Value

If _Region 0 Then 

Dim _Conn 
As SqlConnection
Dim _Command 
As SqlCommand
Dim _Reader 
As SqlDataReader
Dim SQL 
As String

SQL 
"select * from Territories where RegionID ='" _Region "'"

_Conn GetConnection()

_Command = new SqlCommand(SQL_Conn)

_Reader _Command.ExecuteReader()

Territory.Items.Clear()
Territory.Items.Add(new ListItem("Select Territory...""0"))


While 
_Reader.Read()
Territory.Items.Add(new ListItem(_Reader("TerritoryDescription").ToString(), _Reader("TerritoryID").ToString()))
End While

txtRegion.Text Regions.SelectedItem.Text 
txtTerritory
.Text "" 
Else

Territory.Items.Clear()
Territory.Items.Add(new ListItem("Select Territory...""0"))
txtRegion.Text ""
txtTerritory.Text ""

End If 

End Sub

Sub OnSelectedTerritoryIndexChanged
(Source As ObjectAs EventArgs)

Dim _Territory As Integer

_Territory 
Territory.SelectedItem.Value

If _Territory 0 Then

txtTerritory
.Text Territory.SelectedItem.Text
Else
txtTerritory.Text ""
End If

End Sub

</SCRIPT>

<FORM runat="server"><asp:DropDownList id=Regions runat="server" 
OnSelectedIndexChanged="OnSelectedRegionIndexChanged" 
AutoPostBack="True"></asp:DropDownList>
<asp:DropDownList id=Territory runat="server" 
OnSelectedIndexChanged="OnSelectedTerritoryIndexChanged" 
AutoPostBack="True"></asp:DropDownList>
<asp:Label id=txtRegion runat="server"></asp:Label>
<asp:Label 
id=txtTerritory 
runat="server"></asp:Label></FORM><!-- Web.Config --><CONFIGURATION><APPSETTINGS><ADD 
value="Server=(local)NetSDK;uid=sa;pwd=;database=Northwind" 
key="ConnectionString" /></APPSETTINGS></CONFIGURATION>

blog comments powered by Disqus
ASP CODE ARTICLES

- ASP Forms
- ASP: The Beginning
- Getting Remote Files With ASP Continued
- Inbox and Outbox Manipulation in ASP
- Relational DropDownList Using VB.NET
- Ad Tracking URL Hits
- Use ViewState to display one record per page...
- Send Email using ASP.NET formatted in HTML
- ASP File Explorer
- ASP/XML Interview questions by Srivatsan Sri...
- Pressing RETURN won't submit the form
- This shows how you get the TEXT of a combo r...
- Group Data by Adrian Forbes
- Multiple checkbox select sample
- Multiple checkbox select with all values sam...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials