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****************************** <!-- ASPX Page --><!-- 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 --><%@ Import Namespace="System.Data" %><%@ Import Namespace="System.Data.SqlClient" %> <SCRIPT language=VB runat="server">
Public Sub Page_Load(Source As Object, E As 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 Object, E As 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 Object, E As 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>
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More ASP Code Articles More By aspfree developerWorks - FREE Tools! | Poor Requirements Management capabilities in an Enterprise have been linked to excessive project failures, escalating IT costs, and failure to deliver competitive advantage into the marketplace. Join Brianna M Smith from IBM Rational and learn about how successful organizations align IT and Business stakeholders through collaborative processes and tools for effective requirements management, and how an integrated approach across the IT lifecycle can provide unparalleled visibility and traceability to ensure that project teams are delivering on the business vision by "doing the right things" and "doing things right." FREE! Go There Now!
| | | | Learn how you can extend modern application lifecycle management to IBM System z through the IBM Rational Software Delivery Platform (SDP). The Did you say mainframe? e-kit includes podcasts, webcasts, tutorials, white and red papers, demos, and articles designed to help ease the challenges of modernizing your enterprise. This complimentary kit for mainframe developers is a practical, how-to guide for making the most of an existing development environment, including the skills and infrastructure already in place at an established enterprise. FREE! Go There Now!
| | | | Download a free trial version of IBM Rational Developer for System i V7.1, which provides a complete development environment for traditional i5/OS application development. IBM Rational Developer for System i is a new eclipse-based workstation offering for i5/OS application development that provides a comprehensive Integrated Development Environment for edit/compile/debug of traditional RPG/COBOL/C/C++ i5/OS applications. FREE! Go There Now!
| | | | Visit IBM developerWorks to download a free trial of the Rational Host Access Transformation Services (HATS) Toolkit. The HATS toolkit provides a set of plug-ins for the IBM Rational Software Delivery Platform to help you easily extend your legacy applications. HATS makes your 3270 and 5250 applications available as HTML through the most popular Web browsers, while converting your host screens to a Web look and feel and it also enables you to develop new Web, portal, and rich-client applications. FREE! Go There Now!
| | | | Ken Krugler, co-founder of code search company Krugle, and Laura Merling, vice president of Marketing and Business Development for Krugle, join to talk about the ins and outs of code search and what it means as a new feature for developerWorks users. FREE! Go There Now!
| | | | Join this Rational Talks to You teleconference on December 6 at 1:00 pm ET to participate in an agile application development discussion and get your questions answered on using IBM Rational Method Composer in a distributed environment.Get your questions answered! FREE! Go There Now!
| | | | Get a free trial download of the latest version of IBM Rational Method Composer V7.2 which helps you deliver customized yet consistent process guidance to your project teams and IT organization, and includes the latest version of IBM Rational Unified Process (RUP), which has provided process guidance to teams since 1996. FREE! Go There Now!
| | | | Join this Rational Talks to You teleconference, to hear how Enterprise Generation Language (EGL) eliminates the need for tedious and error-prone low level coding, so developers can focus on business requirements. EGL extends the Rational software development platform with a simplified programming language that enables developers who have little or no experience with Java, Web technologies or Service Oriented Architecture, to create enterprise-class applications and services quickly and easily. It also allows developers who may have little or no mainframe programming experience to quickly create traditional mainframe components. FREE! Go There Now!
| | | | The Eclipse community is constantly working to extend Eclipse's functionality. In this webcast, learn about some of the most important and feature-rich projects under development. From multi-language support to plug-in development, tune in to see what Eclipse is capable of now. FREE! Go There Now!
| | | | With IBM Rational Systems Development Solution, you can deliver products faster with higher quality. Within this kit, Read the “Model Driven Systems Development” white paper to see how to improve product quality and communication. Then check out the rest of the e-Kit to learn more about important topics that can affect the success of any software project through customer examples, tutorials, informative Webcasts, and best practices for designing, building and managing systems. From start to finish, at every stage in your projects, Rational Systems Development Solution can help your company reach its full potential. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |