use index on dropdownlist

Discussion in 'ASP .Net Web Controls' started by Sam, Oct 25, 2007.

  1. Sam

    Sam Guest

    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
    Sam, Oct 25, 2007
    #1
    1. Advertising

  2. Sam

    Phil H Guest

    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
    Phil H, Oct 25, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. karthikeyavenkat
    Replies:
    2
    Views:
    565
    Bryce
    Mar 17, 2005
  2. Shawn W_
    Replies:
    5
    Views:
    263
    Aldric Giacomoni
    Sep 16, 2009
  3. ngoc
    Replies:
    5
    Views:
    166
    Tad McClellan
    May 11, 2006
  4. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    268
    Tomasz Chmielewski
    Mar 4, 2008
  5. Stefan Mueller
    Replies:
    7
    Views:
    176
    Thomas 'PointedEars' Lahn
    Nov 28, 2005
Loading...

Share This Page