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

SqlDataReader.Read

The following code calls a function in a DLL which returns a
SqlDataReader (the function in the DLL calls a stored procedure which
returns 6 columns):

Dim boCart As Cart
Dim sqlReader As SqlDataReader

boCart = New Cart
boCart.GetRecords(iUserID)

'Next I want to just display the record in the last column (whose
ordinal no=5) corresponding to the UserID

While (sqlReader.Read)
Response.Write(sqlReader.GetValue(5))
End While

The above does display the appropriate record. Next I want to display
the rest of the records in a DataList but when I do

dlCart.DataSource = sqlReader
dlCart.DataBind()

the DataList doesn't display the records. How do I populate the
DataList with the rest of the records that the stored procedure
retrieves?

Oct 6 '06 #1
2 5358
You've already read through the entire result set. Now it's done. You have
to rerun the query again if you want to bind to it. Or, you have to use a
more persistent data storage mechanism such as a datatable, which keeps the
result set in memory. The datareader just fetches the results from the
database as you request each row, then throws the old one away.

<rn**@rediffmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
The following code calls a function in a DLL which returns a
SqlDataReader (the function in the DLL calls a stored procedure which
returns 6 columns):

Dim boCart As Cart
Dim sqlReader As SqlDataReader

boCart = New Cart
boCart.GetRecords(iUserID)

'Next I want to just display the record in the last column (whose
ordinal no=5) corresponding to the UserID

While (sqlReader.Read)
Response.Write(sqlReader.GetValue(5))
End While

The above does display the appropriate record. Next I want to display
the rest of the records in a DataList but when I do

dlCart.DataSource = sqlReader
dlCart.DataBind()

the DataList doesn't display the records. How do I populate the
DataList with the rest of the records that the stored procedure
retrieves?

Oct 6 '06 #2
<rn**@rediffmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
The following code calls a function in a DLL which returns a
SqlDataReader (the function in the DLL calls a stored procedure which
returns 6 columns):

Dim boCart As Cart
Dim sqlReader As SqlDataReader

boCart = New Cart
boCart.GetRecords(iUserID)

'Next I want to just display the record in the last column (whose
ordinal no=5) corresponding to the UserID

While (sqlReader.Read)
Response.Write(sqlReader.GetValue(5))
End While

The above does display the appropriate record. Next I want to display
the rest of the records in a DataList but when I do

dlCart.DataSource = sqlReader
dlCart.DataBind()

the DataList doesn't display the records.
That's correct - because you've already iterated right through the
DataReader in your While loop. DataReader objects are read-only
forward-scrolling only - once you've got to the end of your DataReader, you
can't go back to the start again and use it for something else.
How do I populate the DataList with the rest of the records that the
stored
procedure retrieves?
Several options...

1) Don't use a DataReader - use a DataSet instead.

2) Bind your dlCart object first, close the DataReader, then iterate through
the dlCart object to do your Response.Write...

3) Query the database twice - really stupid idea, but of course it would
solve your problem!

Plus many other options, I'm sure...
Oct 6 '06 #3

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

Similar topics

3
by: Matt | last post by:
I am using SQLDataReader to do a rather simple select statement, and then running an update in the while loop for the Read method. When I place the SQLDataReader in a thread delegate or not, it...
7
by: Franck Diastein | last post by:
Hi, when I call ExportData I have this error: Invalid attempt to Read when reader is closed. Telling me that there's a problem with this line: while(_dataR.Read()){ Code:...
3
by: Neil Guyette | last post by:
Hello, Everyone, I'm trying to find information on how to populate a combo box using a SqlDataReader. I want to be able to set the value of the combo's value property different then the...
1
by: Arvind P Rangan | last post by:
Hi All, How do you get all the values of a sqldatareader if it contains multiple resultset. Using sqldatareader.nextresult and sqldatareader.read e.g. While sqldatareader.read ' If not...
8
by: bidllc | last post by:
I have a funtion that works fine and dandy when called from anywhere in my app. It will NOT work when called from inside the class in which it resides. This is the function I'm calling:...
4
by: mimi | last post by:
Hi Please help me out, I can't find a way to close a sqldatareader when error occur at statement cmd.ExecuteReader(). I can't close it in catch because it is local in try scope and I can't...
7
by: Web learner | last post by:
I am trying to create a method GetDataFor(string column) becaues I have to repeat the same statements for several columns but I get an error as follows: The name 'dr' does not exist in the current...
3
by: Jon Haakon Ariansen | last post by:
Hi, I realize that it's not possible to get the rowcount from SqlDataReader, but if you have a stored procedure where you return @@ROWCOUNT it should be possible to get the rowcount through the...
4
by: Cirene | last post by:
I have a sqldatareader that I use to read some data. Later I do a dr.close. In the same sub I later to "dr = MyCommand.ExecuteReader" because I'm trying to reuse the var with a totally different...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: 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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.