Populate a dropdown list according to option selected in another dropdown list

liunx

Guest
:confused: :( I have populated a dropdown list with the data in a database (finally), but I need to be able to populate a second dropdown list according to the results of the first.

EG First dropdown list = US, UK, SPAIN
Second dropdown list = Ireland, Scotland, Wales, England (presuming UK was selected)

The items from the second list need to come from the database????????????????????????????????You are going to have to post this back to the server to do this and then just use an sql statement another another dataset for tha second list.

You could just hardcore the items from the second list into your page and use an if statement... but data seems like it would be easier. You do not even need to make a new one, just put a new table in the existing one named providences or something like that.Please help it's really annoying me now!!!!

:( I need the following code to display a list in the first box with items from the database, which when clicked, display the correct information in the datagrid. I can't see why it wont work! (Please ignore stars - this is not the problem)

Imports System.Data.SqlClient
Public Class droptest
Inherits System.Web.UI.Page

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
'Connect to database

Dim myconnection As SqlConnection
Dim myda As SqlDataAdapter
Dim ds As DataSet

myconnection = New SqlConnection("workstation id=****;packet size=4096;integrated security=SSPI;data source=""****/****"";persist security info=False;initial catalog=name")
myda = New SqlDataAdapter("Select DISTINCT * from name ", myconnection)
ds = New DataSet
myda.Fill(ds, "name")
'Populate list with categories from database
DropDownList1.DataSource = ds
DropDownList1.DataSource = ds.Tables(0)
DropDownList1.DataTextField = ds.Tables(0).Columns("Category").ColumnName.ToString()
DropDownList1.DataValueField = ds.Tables(0).Columns("Category").ColumnName.ToString()
DropDownList1.DataBind()

End Sub

End Class

Public Class question
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myDataset As New DataSet
'Display results in grid according to selection

Dim DS As DataSet
DS = New DataSet

SqlDataAdapter1.SelectCommand.CommandText = "SELECT * FROM name WHERE Category='" & DropDownList1.SelectedValue & "'"

SqlDataAdapter1.Fill(DS, "name")



DataGrid1.DataSource = DS.Tables("name").DefaultView
DataGrid1.DataBind()
End Sub

End Class
 
Back
Top