473,804 Members | 3,058 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(x lRow, 1) = .Item("emp_name ")
xlSheet.Cells(x lRow, 2) = .Item("dt_filed ")
xlSheet.Cells(x lRow, 3) = .Item("dt_check ")
xlSheet.Cells(x lRow, 4) = .Item("g_type")
xlSheet.Cells(x lRow, 5) = .Item("g_desc")
xlSheet.Cells(x lRow, 6) = .Item("totPay")
xlSheet.Cells(x lRow, 7) = .Item("dept")
xlSheet.Cells(x lRow, 8) = .Item("union_na me")
xlRow = xlRow + 1
.NextResult()
Loop
End With
reader.Close()

I appreciate any help.

Jan 17 '06 #1
3 21500
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(x lRow, 1) = .Item("emp_name ")
xlSheet.Cells(x lRow, 2) = .Item("dt_filed ")
xlSheet.Cells(x lRow, 3) = .Item("dt_check ")
xlSheet.Cells(x lRow, 4) = .Item("g_type")
xlSheet.Cells(x lRow, 5) = .Item("g_desc")
xlSheet.Cells(x lRow, 6) = .Item("totPay")
xlSheet.Cells(x lRow, 7) = .Item("dept")
xlSheet.Cells(x lRow, 8) = .Item("union_na me")
xlRow = xlRow + 1
Loop
End With
reader.Close()

Kerry Moorman

"tj*****@ph enom-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(x lRow, 1) = .Item("emp_name ")
xlSheet.Cells(x lRow, 2) = .Item("dt_filed ")
xlSheet.Cells(x lRow, 3) = .Item("dt_check ")
xlSheet.Cells(x lRow, 4) = .Item("g_type")
xlSheet.Cells(x lRow, 5) = .Item("g_desc")
xlSheet.Cells(x lRow, 6) = .Item("totPay")
xlSheet.Cells(x lRow, 7) = .Item("dept")
xlSheet.Cells(x lRow, 8) = .Item("union_na me")
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
467
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. I find that the second one always is blank. I assume this is because the DataReader is read-forward only. ******************************************************** connect.open() cmdSelect = New OleDbCommand("exec populateCities 'North...
2
1423
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
3355
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 had written out from my web apps. I can get my DataReader to read my data fine. But I wanted to use a DataGrid, but it is apparently completely different???? I can do:
3
1517
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 with Sub-Tables and Summaries, etc., etc. Here's my question: Is there a way to use any of the ASP.NET 2.0 Data Controls - or any other mechanism - to display the DataReader results without having to define all of the columns and all that in...
2
5030
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 VarType(dtrList) != VariantType.Null And dtrList = True Then chkAssignedGroups.Items.Selected = True End If
3
5413
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 being used is quite different from usual column-row component like Grid component. If you kind a familiar using listView, u must have known what i'm talking about. Here is some example i took from MSDN :...
7
3400
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 internally the DataAdapter uses the DataReader... so whats the deal of writing a method that converts a DataReader into a DataTable ? Thanks!
4
2437
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 2 1 3 1 4 2 1
3
2471
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 problem for one of the operations I am doing. I have a table with hierarhical data (only 3 levels deep) that I would like to parse through in a depth-first search. Each row in the table in the database has a Name and a ParentName, and I'm doing...
0
9569
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10558
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10302
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5503
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2975
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.