Populating DropDownList Items

J

Joe Delphi

Hi,

I am trying to populate dropdownlist items from the results of a
database query. My code looks like this:

Private Sub PopulateSubByBox(ByVal DBConn As SqlConnection)
Dim DBComm As New SqlCommand
Dim DBReader As SqlDataReader
DBComm.Connection = DBConn
DBComm.CommandText = "SELECT * FROM PERSONNEL ORDER BY PERSONNEL"
DBReader = DBComm.ExecuteReader()

'Now Populate the ddlstSubBy.Items with the results from the DBReader - '
ddlstSubBy.Items(0) = DBReader.GetString(0)
End Sub

I get an error on the last line that ddlstSubBy.Items is a Read-Only
property.

So if it is Read-Only, how do I populate it?


JD
 
K

Karl Seguin

(A) you should use items.Add()

(b) you should really use DataBinding

ddlstSubBy.DataSource = DBReader
ddlstSubBy.DataTextField = "ColumnName to Display"
ddlstSubBy.DataValueField = "ColumnValue"
ddlstSubBy.DataBind()


(c) you should dispose of your connection, command and close your reader.


Karl
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top