SortedLists and Databinding

  • Thread starter Thread starter Ben Mann
  • Start date Start date
B

Ben Mann

Hi There,

Ive got an ASP .Net page with a listbox element.
This listbox element is databound to a sorted list filled with keys and
values from the database:


dbReader = lookupUtil.getLookup("country", 0, 0,
ConfigurationSettings.AppSettings("DSN"))
If dbReader.HasRows Then
countries = New SortedList
countries .Add("", "Please Select")
While dbReader.Read
countries .Add(CStr(dbReader.GetInt32(0)), dbReader.GetString(1))
End While
End If


lstCountry.DataSource = countries
lstCountry.DataValueField = "key"
lstCountry.DataTextField = "value"
lstCountry.DataBind()


Im using a sortedlist because the documentation led me to believe that the
items will come out of the list int he same order that they went in (like an
array), but have key/value pairs to store both id's and values in the data
structure.

I know that the information is coming out ordered correctly from the DB, but
the listbox values are completely jumbled up.

Am i missing a setting for the SortedList as it seems to be behaving like a
HashTable at the moment.

Many Thanks.

Ben Mann.
 
Why don't you just get back the values from the database, as you're
currently doing in the correct order, and bind that to your
dropdownlist/<select> element (I assume that's what lstCountry is).

Then you just insert a new listitem at position 0

lstCountry.Items.Insert(0, New ListItem("Please Select", ""))

Cheers
Ken


: Hi There,
:
: Ive got an ASP .Net page with a listbox element.
: This listbox element is databound to a sorted list filled with keys and
: values from the database:
:
:
: dbReader = lookupUtil.getLookup("country", 0, 0,
: ConfigurationSettings.AppSettings("DSN"))
: If dbReader.HasRows Then
: countries = New SortedList
: countries .Add("", "Please Select")
: While dbReader.Read
: countries .Add(CStr(dbReader.GetInt32(0)), dbReader.GetString(1))
: End While
: End If
:
:
: lstCountry.DataSource = countries
: lstCountry.DataValueField = "key"
: lstCountry.DataTextField = "value"
: lstCountry.DataBind()
:
:
: Im using a sortedlist because the documentation led me to believe that the
: items will come out of the list int he same order that they went in (like
an
: array), but have key/value pairs to store both id's and values in the data
: structure.
:
: I know that the information is coming out ordered correctly from the DB,
but
: the listbox values are completely jumbled up.
:
: Am i missing a setting for the SortedList as it seems to be behaving like
a
: HashTable at the moment.
:
: Many Thanks.
:
: Ben Mann.
:
:
 
Hi Ken

Thanks for that. It sounds like the right way to go.
But one extra question is....

Ive been thinking about storing the SortedList that ive been generating in
the Cache for an hour (or some other amount of time) to save querying the DB
on every page load. Is it feasable to do the same thing with a
SQLDataReader?

Thanks.

Ben Mann.
 
Back
Top