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! | Download a free trial version of IBM DB2 9.5 for Linux, UNIX, and Windows. DB2 9 is the result of a five-year development project that transformed traditional (static) database technology into an interactive data server that merges the high performance and ease of use of DB2 with the self-describing benefits of XML. FREE! Go There Now!
| | | | XML has become a common way of storing business data as flat files and many data server vendors including IBM have provided ways to store this data within relational database systems. Increasingly collections of XML files are accessed like databases using an xQuery and other XML standard mechanisms. Businesses find the need to combine the traditional tabular structured data with XML formatted data. In this webcast, you’ll learn about IBM’s WebSphere Federation Server technology, which provides users with the ability to integrate these two data formats. FREE! Go There Now!
| | | | As organizations integrate software into every aspect of business, they are constantly pressured to deliver faster, better, and cheaper results. Unfortunately, a “dis-integrated” software delivery approach reduces returns while increasing costs. This IBM Rational White Paper shows how Integrated Requirements Management aligns organizations around maximizing value and keeping pace with change. 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!
| | | | User communities play an important role in communication and collaboration around products, solutions and other areas of special interest to members. Successful communities are able to provide the right mix of content and services to deliver a value proposition that resonates with each audience. Join Tom Inman, VP of Marketing for Information and Platform Solutions as he introduces the new LeverageINFORMATION community. During this webcast, learn about the value provided by the community and how customers and partners derive value from the community in addressing their own technical and business challenges. FREE! Go There Now!
| | | | Learn field-tested SOA principles, methodology, technology and implementation from the global SOA market leader - in a new e-book by an IBM SOA expert. Written by IBM Certified SOA Solution Designer Bobby Woolf, "Exploring IBM SOA Technology & Practice" is the ultimate insider's guide to SOA - a PDF e-book packed cover to cover with IBM's specific advice on how to make your SOA implementation a success. 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!
| | | | <a href="http://zeus.developershed.com/shonuff.php?blackbird=3853&zoneid=442&source=&dest=http%3A%2F%2Fwww.ibm.com%2Fdeveloperworks%2Fspaces%2Fjazz%3FS_TACT%3D105AGY31%26S_CMP%3DDEVSHED&ismap="><img src="http://images.devshed.com/corp/img/news/jazz01.gif" alt="developerWorks Jazz space" align="left"></a>You've heard the buzz about Jazz... want to know more about it from a developer's perspective? Check out the Jazz space on developerWorks. This space is an up-to-date resource for developers, including technical information about Jazz and products built on Jazz, like Rational Team Concert Express. The Jazz space includes content from a wide variety of sources, including links, feeds, and comments from experts. FREE! Go There Now!
| | | | Join this webcast to see how IBM Data Studio Developer and pureQuery can take the pain out of Java data access. uApplications developed using both Java and SQL have become a common requirement. Database connectivity using Java Database Connectivity (JDBC) to create an application is a multi-step tedious process, and tooling that covers both SQL and Java has been unavailable, until now. IBM Data Studio introduces the pureQuery platform: a high-performance, Java data access platform focused on simplifying the tasks of developing, managing, and optimizing database applications and services. FREE! Go There Now!
| | | | Attend this launch webcast with Scott Hebner, Vice President of IBM Rational Marketing and Strategy, for an overview of Rational’s new software offerings and resources to help modernize and accelerate software innovation on i on Power Systems – while ensuring past application investments are protected and continue to grow. Learn how these solutions are helping customers extend their core i5/OS solutions toward modern architectures such as SOA and web technologies to deliver business improvements that stand the test of time. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |