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! | This demonstration gives you an overview of IBM® Rational® Build Forge Express Edition, a global offering that provides a framework to automate and execute software processes. Rational Build Forge provides a software assembly line that can support all of your tools, technologies, and platforms so you can achieve a repeatable, reliable, and traceable build and release process. FREE! Go There Now!
| | | | You probably have thousands of lines of COBOL code loaded with business intelligence and being used to run your business, along with an army of developers maintaining these applications. Learn how to prepare your applications and developers so you can keep that competitive edge and move to a service-oriented architecture with the IBM Rational Enterprise Modernization solutions. Replay is available for 9 months. FREE! Go There Now!
| | | | Set up a PHP Web interface for the Java(TM) business application using a database created in earlier in this series. The PHP Web interface collects information from users and sends the session data to the Java business application for processing and for a response. FREE! Go There Now!
| | | | Download a free trial version of IBM Rational Developer for System z, software that can help you deliver core development capabilities; the power of Java Platform, Enterprise Edition (Java EE); and rapid application development support to diverse enterprise application development teams. With comprehensive development tools to help create, deploy and maintain traditional enterprise and composite applications, Rational Developer for System z enables developers with different technical backgrounds to easily participate in important technology projects. FREE! Go There Now!
| | | | As systems increase in complexity, communication between systems and software teams becomes more and more difficult. Now, there’s a way to improve product quality and communication.<br />Read the “Model Driven Systems Development” white paper to see how. Also included in this kit are more educational white papers, customer examples, tutorials, informative Webcasts, and best practices for designing, building and managing systems.<br /> FREE! Go There Now!
| | | | Listen to this webcast to get an overview of Info 2.0 and a technical demo of how to quickly build an enterprise mashup. IBM's Info 2.0 technology leverages emerging Web 2.0 technologies such as mashups, feeds, AJAX, and JSON in order to simplify assembly of information using feeds and services. Come learn about the technical elements of Info 2.0 including the Feed Generation framework, Mashup Engine, and mashup assembly components. Learn how to pull information from databases, departmental information, and the Web to create mashups critical to your company’s success. We will also discuss best practices to help you get started. FREE! Go There Now!
| | | | To create, test, and deploy a Web-based application or Web service rapidly, you need a proven relational database, a standards-compliant Web application server, and a flexible IDE. Ideally, all these software packages are production-tested, simple to obtain, easy to use, and well integrated with one another. This tutorial shows you how to use IBM-backed open source and free software to kick-start your Java Web-based application development. You'll learn exactly where to download such components, install them, and get them working for you today. FREE! Go There Now!
| | | | Learn the basics of the IBM Customer Information Control System (CICS). With a hands-on exercise, learn how to get your first CICS application up and running on your desktop using TXSeries V6.1 for Windows. The tutorial shows you how to download and install a free trial version of TXSeries V6.1. FREE! Go There Now!
| | | | Here's a fun way to learn about DB2! Learn or teach the basics of DB2 and relational database with an interactive game called The DB2 Detective Game. The game teaches relational database concepts and shows how technology can be applied to solving real-life problems (the game's theme is a crime investigation). This tutorial has been updated for DB2 9. FREE! Go There Now!
| | | | Investigate the effects of field-level caching in dynamic array access, in part 5 of the UniVerse performance series. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |