Solved

VB.Net - DataReader Looping

Posted on 2013-01-28
3
406 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
ID: 38827179
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
ID: 38830185
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
ID: 38831162
Thanks for the feedback as I continue my journey.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

867 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

17 Experts available now in Live!

Get 1:1 Help Now