Code Bank
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingCode Bank

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
  #1  
Old February 5th, 2004, 02:19 PM
Rich's Avatar
Rich Rich is offline
Administrator
Developer Shed Admin.
 
Join Date: Sep 2003
Location: Fort Lauderdale, FL
Posts: 151 Rich User rank is Sergeant (500 - 2000 Reputation Level)Rich User rank is Sergeant (500 - 2000 Reputation Level)Rich User rank is Sergeant (500 - 2000 Reputation Level)Rich User rank is Sergeant (500 - 2000 Reputation Level)Rich User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 11 h 37 sec
Reputation Power: 10
Relational DropDownList Using VB.NET

Relational DropDownList Using VB.NET Code Example.******************************

<!-- 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" %>

<html>
<head>
<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>
</head>
<body>
<form runat="server">
<aspropDownList id="Regions" AutoPostBack="True" OnSelectedIndexChanged="OnSelectedRegionIndexChanged" runat="server" />
<p />
<aspropDownList id="Territory" AutoPostBack="True" OnSelectedIndexChanged="OnSelectedTerritoryIndexChanged" runat="server" />
<p />
<asp:Label id="txtRegion" runat="server" />
<br />
<asp:Label id="txtTerritory" runat="server" />
</form>
</body>
</html>

<!-- Web.Config -->

<configuration>
<appSettings>
<add key="ConnectionString" value="Server=(local)NetSDK;uid=sa;pwd=;database=Northwin d" />
</appSettings>
</configuration>

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingCode Bank > Relational DropDownList Using VB.NET


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway