Looking for suggestions on populating dropdown from database table

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: pantichd (pantichd_at_firstenergycorp.com)
Date: 02/09/05


Date: Wed, 9 Feb 2005 08:47:08 -0500

Hello,

I want to populate an employee name dropdown in a webform with values from a database table. I know I can use a data adapter to retrieve the whole table into a dataset and bind the table from the dataset to the dropdown. However, I thought I could do it a little more efficiently by using a datareader to retrieve the name and id columns from the table, put them in a collection and then bind the collection to the dropdown.

Below is the method I'm using to retrieve a key/value pair from a database table. It returns a collection. Well, now I'm stuck. I can't figure out how to get the collection into the dropdown.

When reading about dropdowns I keep running into documentation about ListItemCollection but I can't figure out how to go from collection to ListItemCollection and then how to get that bound to the dropdown.

Any help would be greatly appreciated.

David

Public Function getKeyValueList(ByVal sql As String) As Collection

    Dim coll As Collection = New Collection

    readerConn.Open()

    readerCmd.Connection = readerConn

    readerCmd.CommandText = sql

    reader = readerCmd.ExecuteReader

    If reader.HasRows Then

    Do While reader.Read()

        coll.Add(reader.GetString(0), reader.GetString(1))

    Loop

    End If

    reader.Close()

    readerConn.Close()

End Function



Relevant Pages

  • RE: Looking for suggestions on populating dropdown from database table
    ... I can't figure out how to get the collection into the dropdown. ... > Public Function getKeyValueList(ByVal sql As String) As Collection ... readerCmd.Connection = readerConn ...
    (microsoft.public.dotnet.framework)
  • Re: Cached DropDownList has items, but wont display. Wheres my error
    ... > I am trying to cache a dropdown list rather than hit the database every ... I was going to cache the data by ... > caching the datareader, ... > Public Function fddPatientsFill(ByVal vlngUserID As Integer, ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)
  • Re: Looking for suggestions on populating dropdown from database table
    ... Gregory, ... I found an example in the documentation that showed something almost exactly ... Even though in this case I want to end up with the data in a dropdown there ... ListItemCollection and then how to get that bound to the dropdown. ...
    (microsoft.public.dotnet.framework)
  • Re: Dropdown is un-responsive
    ... I'll give that a whirl. ... I suppose it could be a scope ... is in a Public Function within the class module, ... successful in getting the combo box to dropdown. ...
    (microsoft.public.access.formscoding)
  • CB_SETTOPINDEX and Combobox
    ... 3rd item from the top when the dropdown is clicked. ... SendMessage CB_TopIndex call work with such a combob?. ... Private Sub cboTimeIncrement_DropDown ... Public Function cbo_set_topindex ...
    (microsoft.public.vb.general.discussion)