use index on dropdownlist

S

Sam

I got a drop down list, but when i try to selected an index it dose not
work. can someone give me some help?

here the code:

Dim dsCountry As New DataSet

dsCountry = GetCountries()


With dpdnCountry

Dim lstItem As System.Web.UI.WebControls.ListItem
Dim i As Int32

lstItem = New System.Web.UI.WebControls.ListItem
lstItem.Value = ""
lstItem.Text = " -- Select -- "
.Items.Add(lstItem)

For i = 0 To dsCountry.Tables(0).Rows.Count - 1
If (Not dsCountry.Tables(0).Rows(i)("description") Is
System.DBNull.Value) And (Not dsCountry.Tables(0).Rows(i)("description") Is
DBNull.Value) Then
lstItem = New System.Web.UI.WebControls.ListItem
lstItem.Value = dsCountry.Tables(0).Rows(i)("id") '
i.e. 1
lstItem.Text =
dsCountry.Tables(0).Rows(i)("description") ' i.e. USA
.Items.Add(lstItem)
End If
Next

dpdnCountry.SelectedIndex() =
dpdnCountry.Items.IndexOf(dpdnCountry.Items.FindByValue("USA"))

end With
 
P

Phil H

Hi Sam

Are you sure you have reproduced this correctly?
If (Not dsCountry.Tables(0).Rows(i)("description") Is
System.DBNull.Value) And (Not dsCountry.Tables(0).Rows(i)("description") Is
DBNull.Value) Then
...

If so why are you testing the same condition twice?

I'm not sure that the "Is" operator in this context will give the
result you are looking for and is probably the reason it isn't working
(the test fails every time and the list remains empty).

Personally I think it's better to work with DataView and DataRowView
objects for reading and updating tables. I recommend something like
this:

Dim dvCountry as DataView = dsCountry.Tables(0).DefaultView
Dim drvCountry as DataRowView

Then in place of your For-Next loop for building the DropDownList try:

For Each drvCountry in dvCountry

If Not IsDBNull(drvCountry("description")) Then

1stItem = New System.Web.UI.WebControls.ListItem
1stItem.Value = drvCountry("id")
1stItem.Text = drvCountry("description")
.Items.Add(1stItem)

end if

Next

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top