473,385 Members | 1,333 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,385 software developers and data experts.

A simple question about drop-down list in ASP.NET

What am I missing here. I bind a drop-down list in ASP.NET placed on a
web form to a DataReader. The binding is done at run-time and not at
design time. Here's the code I write to bind the list:
Private Sub FillRoles()
ddlRole.Items.Clear()

ddlRole.DataSource =
DataServices.DataProvider.GetReader("select [Role ID], [Role Name] FROM
[Role] ORDER BY [Role Name]")
ddlRole.DataBind()
ddlRole.DataMember = "Role"
ddlRole.DataTextField = "Role Name"
ddlRole.DataValueField = "Role ID"
End Sub
However, instead of the role names, what I get in the list is the
string, "System.Data.Common.DbDataRecord" as many times as there are
records.

When I change the code to this:

Private Sub FillRoles()
ddlRole.Items.Clear()

Dim Reader As SqlClient.SqlDataReader =
DataServices.DataProvider.GetReader("select [Role ID], [Role Name] FROM
[Role] ORDER BY [Role Name]")
ddlRole.DataSource = Reader
ddlRole.DataTextField = Reader("Role Name")
ddlRole.DataValueField = Reader("Role ID")
ddlRole.DataBind()
End Sub

it gives me an exception saying, Invalid attempt to read data when no
data is present.

The table does have six records.

I know I am missing a very minute detail here. What is it?

Nov 19 '05 #1
2 1398
Jon
Make ddlRole.DataBind() the last command in the sub

Jon

"Water Cooler v2" <wt*****@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
What am I missing here. I bind a drop-down list in ASP.NET placed on a
web form to a DataReader. The binding is done at run-time and not at
design time. Here's the code I write to bind the list:
Private Sub FillRoles()
ddlRole.Items.Clear()

ddlRole.DataSource =
DataServices.DataProvider.GetReader("select [Role ID], [Role Name] FROM
[Role] ORDER BY [Role Name]")
ddlRole.DataBind()
ddlRole.DataMember = "Role"
ddlRole.DataTextField = "Role Name"
ddlRole.DataValueField = "Role ID"
End Sub
However, instead of the role names, what I get in the list is the
string, "System.Data.Common.DbDataRecord" as many times as there are
records.

When I change the code to this:

Private Sub FillRoles()
ddlRole.Items.Clear()

Dim Reader As SqlClient.SqlDataReader =
DataServices.DataProvider.GetReader("select [Role ID], [Role Name] FROM
[Role] ORDER BY [Role Name]")
ddlRole.DataSource = Reader
ddlRole.DataTextField = Reader("Role Name")
ddlRole.DataValueField = Reader("Role ID")
ddlRole.DataBind()
End Sub

it gives me an exception saying, Invalid attempt to read data when no
data is present.

The table does have six records.

I know I am missing a very minute detail here. What is it?

Nov 19 '05 #2
Indeed.. you are missing a minute detail :)
You should use ddlRole.DataBind() after specifying the datatextfield and
datavaluefield so the dropdown can understand which columns it should take.
--
HTH
srini
http://www.expertszone.com
"Water Cooler v2" wrote:
What am I missing here. I bind a drop-down list in ASP.NET placed on a
web form to a DataReader. The binding is done at run-time and not at
design time. Here's the code I write to bind the list:
Private Sub FillRoles()
ddlRole.Items.Clear()

ddlRole.DataSource =
DataServices.DataProvider.GetReader("select [Role ID], [Role Name] FROM
[Role] ORDER BY [Role Name]")
ddlRole.DataBind()
ddlRole.DataMember = "Role"
ddlRole.DataTextField = "Role Name"
ddlRole.DataValueField = "Role ID"
End Sub
However, instead of the role names, what I get in the list is the
string, "System.Data.Common.DbDataRecord" as many times as there are
records.

When I change the code to this:

Private Sub FillRoles()
ddlRole.Items.Clear()

Dim Reader As SqlClient.SqlDataReader =
DataServices.DataProvider.GetReader("select [Role ID], [Role Name] FROM
[Role] ORDER BY [Role Name]")
ddlRole.DataSource = Reader
ddlRole.DataTextField = Reader("Role Name")
ddlRole.DataValueField = Reader("Role ID")
ddlRole.DataBind()
End Sub

it gives me an exception saying, Invalid attempt to read data when no
data is present.

The table does have six records.

I know I am missing a very minute detail here. What is it?

Nov 19 '05 #3

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

Similar topics

4
by: Bruce D | last post by:
I have this simple subquery...and I get an error when I try to run it?? SELECT ZipID FROM testzip WHERE ZipID <> ( SELECT ZipID FROM Zip ) any ideas?
5
by: Firehawk® | last post by:
Hi, I have a great experience with DB (Oracle and others) and I am using DB2 now. Then, I have few questions: 1. How drop a schema (user) and all objects of this schema, like a Oracle "drop...
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
19
by: Dales | last post by:
I have a custom control that builds what we refer to as "Formlets" around some content in a page. These are basically content "wrapper" sections that are tables that have a colored header and...
6
by: Ben | last post by:
I'm designing a fairly involved database system. As part fo the process, I use the \i command a great deal. I set up fairly involved queries, sometimes simply for the purpose of shortening column...
20
by: Brian Tkatch | last post by:
An ORDER BY a simple-integer inside a FUNCTION, results in SQL0440N, unless the FUNCTION expects an INTEGER as its parameter. For example: DECLARE GLOBAL TEMPORARY TABLE A(A CHAR(1)) INSERT INTO...
5
by: sesar | last post by:
How can I impement drag&drop for simple 'dialog base' application... I want to drag&drop txt file and load the text to variable
11
by: juvenuts | last post by:
Hi, I'm a complete newbie to C, but I wanted to get started by writing a few simple programs that print out strings. 1. The first thing I wanted to do was write a program that uses getchar to...
10
true911m
by: true911m | last post by:
This is a simple walkthrough to get PyInstaller up and running. I decided to give PI a try, because it claims to be more selective about what it bundles into its executable files by default, and...
1
by: Steve Bottoms | last post by:
Hi, all! Using VB .Net 2k5 under Vista Business... I'm trying to put together a very basic drag-and-drop for file copying, and can't seem to get DragDrop events (Form, PictureBox, TextBox, etc)...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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...

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.