Home All Groups Group Topic Archive Search About
Author
25 Oct 2007 5:53 PM
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
--
Thanks,
Sam

Author
25 Oct 2007 8:57 PM
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

AddThis Social Bookmark Button