Solved

VB.Net - DataReader Looping

Posted on 2013-01-28
3
392 Views
Last Modified: 2013-01-29
Good Day Experts!

What is your preference to looping through a DataReader?  

I have seen:

While reader.Read()

End While

**************************

With reader
     .Read()
     Do While .HasRows

     Loop
End With

What are the pros/cons if any between the 2?

Thanks,
jimbo99999
0
Comment
Question by:Jimbo99999
3 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 75 total points
Comment Utility
I don't think there is any difference between the two approaches. Infact I suspect #2 will throw an error if there are no rows. I prefer the #1 approach.
0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 25 total points
Comment Utility
CodeCruiser has it, there is not difference, but you should use the first set condition if you want to go the Loop route: Do While .Read().

There is something of a was between the proponents of the While and those who would rather use the Loop.

There is only one form of While...End While.

But there are 4 variations of the Do...Loop:
    Do While...Loop
    Do...Loop While
    Do Until...Loop
    Do...Loop Until

Those who prefer the Do...Loop argue that it gives you more control. I disagree with that. What I have seen in many years of code revision and debugging other programmers code is that those who use it often make mistakes. They think that they are in one form while they are in the other. This often end up missing one loop or doing one too many.

There is nothing that you can do with the Do...Loop that you cannot do with the While...End While. Just adjust the expression accordingly. But in my opinion, programmers who use the While...End While have only one way of seeing that kind of loop. They are used to it, and they do less mistakes.
0
 

Author Closing Comment

by:Jimbo99999
Comment Utility
Thanks for the feedback as I continue my journey.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now