Binding data to a DropDownList

S

Steven K

Hello,

I am using the following to bind data from a stored procedure to a
DropDownList. The problem I am having is that instead of getting the data,
I am getting the text "System.Data.Common.DbDataRecord" for every record,
even though if I use a loop, I get the correct information.


Dim spBusUnit As OleDb.OleDbDataReader
Dim prmBusUnit As OleDbParameter
Dim cmdBusUnit As New OleDb.OleDbCommand("sp_Search", cnnSearch)

Data Binding
-------------
spBusUnit = cmdBusUnit.ExecuteReader()
cboBusUnitBox.DataSource = spBusUnit
cboBusUnitBox.DataBind()
spBusUnit.Close() : spBusUnit = Nothing

Looping through data
----------------------
spBusUnit = cmdBusUnit.ExecuteReader()
Do While spBusUnit.Read
strListBox = spBusUnit("BusinessProduct_ID")
strBusUnitBox = strBusUnitBox & "<option value=" & strQuote & strListBox &
strQuote & ">" & strListBox & "</option>"
Loop
spBusUnit.Close() : spBusUnit = Nothing
 
C

Chris Taylor

Hi,

You should also set the DataTextField and DataValueField properties, so that
the controls know which columns to bind for the text and values.

Hope this helps
 
M

Marina

That is because you didn't set the text and value columns that should be
used for the text and value of the dropdown.
 
G

Guest

With a server side DropDownList control .NET can help you databind. You don't have to manually create each option item and value to create the dropdownlist values

Try the following

Create a ASP.NET dropdownlist in the aspx page

Then in the codebehind or inside server script tags in the aspx populate the list

'Data access code will go her
objConn.Open(
Dim objRead As SqlDataReader= objComd.ExecuteReader(

If (objRead.Read()) The
ddl1.DataSource = objRea
ddl1.DataTextField = "yourtextcolumn
ddl1.DataValueField = "yourvaluecolumn
ddl1.DataBind(
End I

objRead.Close(

HTH
Suresh

----- Steven K wrote: ----

Hello

I am using the following to bind data from a stored procedure to
DropDownList. The problem I am having is that instead of getting the data
I am getting the text "System.Data.Common.DbDataRecord" for every record
even though if I use a loop, I get the correct information


Dim spBusUnit As OleDb.OleDbDataReade
Dim prmBusUnit As OleDbParamete
Dim cmdBusUnit As New OleDb.OleDbCommand("sp_Search", cnnSearch

Data Bindin
------------
spBusUnit = cmdBusUnit.ExecuteReader(
cboBusUnitBox.DataSource = spBusUni
cboBusUnitBox.DataBind(
spBusUnit.Close() : spBusUnit = Nothin

Looping through dat
---------------------
spBusUnit = cmdBusUnit.ExecuteReader(
Do While spBusUnit.Rea
strListBox = spBusUnit("BusinessProduct_ID"
strBusUnitBox = strBusUnitBox & "<option value=" & strQuote & strListBox
strQuote & ">" & strListBox & "</option>
Loo
spBusUnit.Close() : spBusUnit = Nothin
 
M

mgwalm

Steven

You seem to be binding the listbox and loading the items uisng code.
You only need to do one or the other (not both)

You would be better binding to a DataSet and setting the properties
DisplayMember and ValueMember.

Look these up in the docs.

Regards
Michael
 

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

Top