473,387 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Populating DropDownList Items

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
Nov 19 '05 #1
1 944
(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

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Joe Delphi" <delphi561@no****@cox.net> wrote in message
news:8uVwe.3745$Qo.766@fed1read01...
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

Nov 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Wim | last post by:
Hi Everyone, I'm trying to speed up a Asp.net 1.1 applications' performance. The application needs to run in an environment with little bandwith and therefore pagesizes and roundtrip times shoud...
3
by: Joe | last post by:
Hello All, I have a UserControl wwhich consists of one Label and one DropDownList control. I have written a Public methofd to allow me to set the Label's Text property and populate the...
0
by: frankiefrank | last post by:
Hello, I have an aspx page with a RadioButtonList that has two Items, and a "ComboBox" (DropDownList) that I want to populate based on the selection in the RadioButtonList. The first...
2
by: RobertTheProgrammer | last post by:
Hi again, Okay, I decided the GridView is the way to go to solve my problem. But now I've got another problem. I've found plenty of examples about how to add a DropDownList item to the grid...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.