PDA

View Full Version : how to make a relationship for 2 drop down list


cool_man77
11-10-2003, 02:26 AM
In database I got 2 table had a relation:

[County]
CountyID
NameCounty

[District]
DistrictID
NameDistrict
CountyID

In webpage I want when user change value on the County drop down list then the District drop down list must be change with CountyID. Have anyone experience with this case? help me pls....

In the show.apsx page I had wrote:

sql="Select * from County Order by Name"
dim mySqlAdapter As SqlDataAdapter
mySqlAdapter= New SqlDataAdapter(sql,myConnection)
dim ds as New DataSet
mySqlAdapter.Fill(ds,"County")
CountyDropDownList.DataSource=ds.Tables("County").defaultView
CountyDropDownList.DataBind()

That code will make a County DropDownList. What I must write for this function? Thanks.

cool_man77
11-10-2003, 06:58 AM
Thanks, mnye.

I have this issues:

After

Dim dataAdapter As SqlDataAdapter = New SqlDataAdapter(sqlCommand)
Dim ds As DataSet = New DataSet
dataAdapter.Fill(dataSet)

How can I count the record in "dataAdapter"??

have the Dataset, SqlDataAdapter, DataReader Object function to count record after run a select query? Thanks for any help!

mnye
11-10-2003, 12:30 PM
cool_man77,

the code you have to populate the country needs to be its own Sub so lets call that ddlCountry, be sure this sub is encased in the Page_Load method and If Not IsPostBack. Also you'll need to add an index value such as:

ddlCountry.DataValueField = ds("CoutryID")

You then want to create a Sub that will populate a dropdown for District. This will be the same except one thing, you want to add an argument called CountryID and a parameter to your SQL statement called @CountryID, like this:

Sub ddlDistrict(Country As Ineger)
sql="SELECT * FROM District WHERE CountyID=@CountyID Order by NameDistrict"

Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
sqlCommand.Parameters.Add("@CountyID", CountyID).Value = orderID
Dim dataAdapter As SqlDataAdapter = New SqlDataAdapter(sqlCommand)
Dim ds As DataSet = New DataSet

dataAdapter.Fill(dataSet)
CountyDropDownList.DataSource=ds.Tables("District").defaultView
CountyDropDownList.DataBind()
End Sub

Then you need to create a function to fire when the selected item changes from the country dropdownlist.

Sub County_SelectedIndexChange(ByVal s As Object, ByVal e As EventArgs)

Dim CountryID As Integer = ddlCountry.SelectedItem.Value

ddlDistrict(CountryID)
End Sub

Then the final step here is to in your country dropdownlist, add the property onselectedindexchanged="SelectedIndexChange"

What will happen is, when the page is loaded, the ddlCountry will be populated, as soon as an item is selected it will pass the country ID to the district selection and populate that drop down.

let me know if you have any issues.

hth
matt

steurm
11-11-2003, 01:28 AM
[b]quote:Originally posted by cool_man77

Thanks, mnye.

I have this issues:

After

Dim dataAdapter As SqlDataAdapter = New SqlDataAdapter(sqlCommand)
Dim ds As DataSet = New DataSet
dataAdapter.Fill(dataSet)

How can I count the record in "dataAdapter"??

have the Dataset, SqlDataAdapter, DataReader Object function to count record after run a select query? Thanks for any help!
</blockquote id="quote"></font id="quote">
I'm not sure how you can count records in a DataAdapter, but you count the records in de dataset using ds.Tables[0].Rows.Count (presuming that there is only 1 datatable in your dataset)

--
Steurm
www.steurm.net/steurm