P
pantichd
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
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