hi

recently, i realise theres such thing as Hashtable that are allow listbox to retrieve url when clicked. However, as i find out more, i realise most examples i saw are static. As in

dim mycountries=New Hashtable
  mycountries.Add("N","Norway")
  mycountries.Add("S","Sweden")
  mycountries.Add("F","France")
  mycountries.Add("I","Italy")

is there any way to retrieve this list from database instead of coding it like that?

i tried doing so, but it gave me a "Public member 'DataSource' on type 'Hashtable' not found." error.

cmdSelect = New OdbcCommand( "SELECT IMG_TITLE, IMG_LINK FROM worklist", conODBC)


DApt.selectCommand = cmdSelect
DApt.Fill(dsSet, "worklist")

Dim linkrd = new Hashtable
linkrd.DataSource = dsSet.Tables("worklist")
linkrd.DataBind()

linklist.DataSource = linkrd
linklist.DataTextField="img_title"
linklist.DataValueField="img_link"
linklist.DataBind()

can anyone guide me on this?
thanks a lot

Dani AI

Generated

Short answer: the Hashtable error happened because a Hashtable is not a server control and therefore has no DataSource property. Two simple approaches work reliably: bind the DataTable directly to the list control, or build a collection (IDictionary or a list of ListItem) from the reader and bind that. pointed you toward the IDictionary route; both are valid depending on whether you need transformation or ordering.

Bind the DataTable directly (simplest):

' after filling a DataTable named dt
myList.DataSource = dt.DefaultView
myList.DataTextField = "Title"   ' replace with your title column
myList.DataValueField = "Url"    ' replace with your link column
myList.DataBind()

Or build a safe, typed collection from a DataReader and bind that (avoids loading a full DataSet when unnecessary):

Dim items As New List(Of System.Web.UI.WebControls.ListItem)

Using conn As New OdbcConnection(connString)
  Using cmd As New OdbcCommand("SELECT Title, Url FROM Worklist", conn)
    conn.Open()
    Using rdr = cmd.ExecuteReader()
      While rdr.Read()
        Dim title = If(rdr.IsDBNull(0), String.Empty, rdr.GetString(0))
        Dim url = If(rdr.IsDBNull(1), String.Empty, rdr.GetString(1))
        items.Add(New ListItem(title, url))
      End While
    End Using
  End Using
End Using

myList.DataSource = items
myList.DataTextField = "Text"
myList.DataValueField = "Value"
myList.DataBind()

Quick tips: check for duplicate keys if you use a Hashtable/Dictionary (duplicates throw), handle DBNulls before calling GetString, use OrderedDictionary or a List if you need stable ordering, prefer Dictionary(Of TKey,TValue) for type safety on .NET 2.0+, and always dispose connections/readers (Using). To navigate on selection, read myList.SelectedValue (or handle SelectedIndexChanged with AutoPostBack = true) and validate the URL before redirecting. Good catch, — and nice direction from .

Recommended Answers

All 2 Replies

You almost have it, use "Key", or "Value" for datasource assignment and a DataReader to populate values from database query...

Dim linkrd = New HashTable()

While reader.Read()
   linkrd.Add(reader.GetString(0), reader.GetString(1))
End While

linklist.DataSource = linkrd
linklist.DataTextField = "Value"
linklist.DataValueField = "Key"
linklist.DataBind()

PS: I'm not a VB developer so if I got something mixed up, I'm sure you'll figure it out.

ooh yea i figured it out. thanks a lot!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.