473,387 Members | 1,834 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.

looping through datareader

Albeit new to VB.NET, I've done this several times before, but today
I've run into an interesting problem. I am trying to loop through a
datareader and display the data in a spreadsheet. So far, I only get 1
row to display. (I know it has multiple rows)
Here's the code for the loop:

xlRow = "4"
With reader
.Read()
Do While .HasRows
xlSheet.Cells(xlRow, 1) = .Item("emp_name")
xlSheet.Cells(xlRow, 2) = .Item("dt_filed")
xlSheet.Cells(xlRow, 3) = .Item("dt_check")
xlSheet.Cells(xlRow, 4) = .Item("g_type")
xlSheet.Cells(xlRow, 5) = .Item("g_desc")
xlSheet.Cells(xlRow, 6) = .Item("totPay")
xlSheet.Cells(xlRow, 7) = .Item("dept")
xlSheet.Cells(xlRow, 8) = .Item("union_name")
xlRow = xlRow + 1
.NextResult()
Loop
End With
reader.Close()

I appreciate any help.

Jan 17 '06 #1
3 21434
tjonsek,

The NextResult method moves to the next result when you are executing
multiple sql statements.

You probably want something like:

With reader
Do While .Read()
xlSheet.Cells(xlRow, 1) = .Item("emp_name")
xlSheet.Cells(xlRow, 2) = .Item("dt_filed")
xlSheet.Cells(xlRow, 3) = .Item("dt_check")
xlSheet.Cells(xlRow, 4) = .Item("g_type")
xlSheet.Cells(xlRow, 5) = .Item("g_desc")
xlSheet.Cells(xlRow, 6) = .Item("totPay")
xlSheet.Cells(xlRow, 7) = .Item("dept")
xlSheet.Cells(xlRow, 8) = .Item("union_name")
xlRow = xlRow + 1
Loop
End With
reader.Close()

Kerry Moorman

"tj*****@phenom-biz.com" wrote:
Albeit new to VB.NET, I've done this several times before, but today
I've run into an interesting problem. I am trying to loop through a
datareader and display the data in a spreadsheet. So far, I only get 1
row to display. (I know it has multiple rows)
Here's the code for the loop:

xlRow = "4"
With reader
.Read()
Do While .HasRows
xlSheet.Cells(xlRow, 1) = .Item("emp_name")
xlSheet.Cells(xlRow, 2) = .Item("dt_filed")
xlSheet.Cells(xlRow, 3) = .Item("dt_check")
xlSheet.Cells(xlRow, 4) = .Item("g_type")
xlSheet.Cells(xlRow, 5) = .Item("g_desc")
xlSheet.Cells(xlRow, 6) = .Item("totPay")
xlSheet.Cells(xlRow, 7) = .Item("dept")
xlSheet.Cells(xlRow, 8) = .Item("union_name")
xlRow = xlRow + 1
.NextResult()
Loop
End With
reader.Close()

I appreciate any help.

Jan 17 '06 #2
Let me explain what Kerry did so that you will understand it.

DataReaders moves to the next record everytime you call the Read method.

In your code you have used

Do While .HasRows

Well, the DataReader will keep having Rows, and it will go in an endless
loop

Do this instead

If .HasRows Then
Do While .Read
bla bla bla
Loop
End If

Regards
Cyril Gupta
Jan 18 '06 #3
Great. Thanks so much. I've been an ASP coder for a long time, finally
convincing everyone here to switch to .NET, but it's up to me to teach
myself. I'm not familiar with datareaders and only just started
utilizing them. What you said Cyril made sense. Thanks, both of you.

Jan 18 '06 #4

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

Similar topics

12
by: Thomas Scheiderich | last post by:
I have 2 dropdowns that are exactly the same and I don't want to re-read for each. Is there a way to do something like below where I read the data, bind to depCity1 and then bind to destCity2. ...
2
by: Zac Maclean | last post by:
I need to loop through a DataReader. The data it contains is in the following format (presented here as CSV, To and From fields are dates): Widget 1, Property 1, To, From
3
by: tshad | last post by:
I am using VS 2003 and trying to build a standalone application. I have been writing all my asp.net work using DW and am just now trying to build a application that will read some of the tables I...
3
by: Alex Maghen | last post by:
Hi. I have a whole bunch of queries that I'll be running and returning in OleDbDataReader objects. Some of these DataReaders will just be a simple singe table result and others will be more complex...
2
by: RSH | last post by:
I have a situation where I am looping through a datareader's rows. An exception is being generated when I attempt to check the value of a Boolean field where it has a null value:\ If...
3
by: Martin Panggabean | last post by:
Hello All, I've kind a logic problem ... I want to fill the listView control in VB.NET with data in my mySql table using Datareader object component. But It seems that the way of how listView...
7
by: Varangian | last post by:
Hi all, the question I want to ask if the conversion of a DataReader to a Table looping through the DataReader is better than using the Fill Method of the DataAdapter... I'm asking because...
4
by: dejavue82 | last post by:
I am trying to use a DataReader to read a table (that was attained from JOINing two other tables) with the following format: questions answers 1 1 1 ...
3
by: =?Utf-8?B?R3JlZyBTdGV2ZW5z?= | last post by:
I am connecting to an Oracle database using an OleDbConnection. I am using DataReader objects to get query results. However, this limits me to only having one reader open at a time, which is a...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.