Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > For Loop Through Recordset

Reply
Thread Tools

For Loop Through Recordset

 
 
!TG
Guest
Posts: n/a
 
      06-23-2005
I currently use Do while loop, but I'd rather use a For Loop though I
have never gotten the hang of them.
Would some one please be so kind as to show me how to loop through a
recordset.
 
Reply With Quote
 
 
 
 
Bob Barrows [MVP]
Guest
Posts: n/a
 
      06-23-2005
!TG wrote:
> I currently use Do while loop, but I'd rather use a For Loop though I
> have never gotten the hang of them.
> Would some one please be so kind as to show me how to loop through a
> recordset.


Why would you rather use a For loop?

Anyways, looping through a recordset may not be the most efficient way for
you to do what you need to do. See here for alternatives:
http://www.aspfaq.com/show.asp?id=2467

Bob Barrows
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.


 
Reply With Quote
 
 
 
 
!TG
Guest
Posts: n/a
 
      06-23-2005
Bob Barrows [MVP] wrote:
> !TG wrote:
>
>>I currently use Do while loop, but I'd rather use a For Loop though I
>>have never gotten the hang of them.
>>Would some one please be so kind as to show me how to loop through a
>>recordset.

>
>
> Why would you rather use a For loop?
>
> Anyways, looping through a recordset may not be the most efficient way for
> you to do what you need to do. See here for alternatives:
> http://www.aspfaq.com/show.asp?id=2467
>
> Bob Barrows

For practice sake
 
Reply With Quote
 
McKirahan
Guest
Posts: n/a
 
      06-23-2005
"!TG" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Bob Barrows [MVP] wrote:
> > !TG wrote:
> >
> >>I currently use Do while loop, but I'd rather use a For Loop though I
> >>have never gotten the hang of them.
> >>Would some one please be so kind as to show me how to loop through a
> >>recordset.

> >
> >
> > Why would you rather use a For loop?
> >
> > Anyways, looping through a recordset may not be the most efficient way

for
> > you to do what you need to do. See here for alternatives:
> > http://www.aspfaq.com/show.asp?id=2467
> >
> > Bob Barrows

> For practice sake


Use GetRows (with a For Loop) instead of looping through a RecordSet:

http://www.learnasp.com/advice/whygetrows.asp


 
Reply With Quote
 
Bob Barrows [MVP]
Guest
Posts: n/a
 
      06-24-2005
!TG wrote:
> Bob Barrows [MVP] wrote:
>> !TG wrote:
>>
>>> I currently use Do while loop, but I'd rather use a For Loop though
>>> I have never gotten the hang of them.
>>> Would some one please be so kind as to show me how to loop through a
>>> recordset.

>>
>>
>> Why would you rather use a For loop?
>>
>> Anyways, looping through a recordset may not be the most efficient
>> way for you to do what you need to do. See here for alternatives:
>> http://www.aspfaq.com/show.asp?id=2467
>>


If, by "For loop" you mean a "For Each" loop, then you are out of louck. A
recordset does not expose its Records collection (which is not really a
collection - note: there is no "Records" property in a Recordset object) via
the IEnumerable interface, so "For Each" cannot be used to loop through the
records of a recordset the way it can be used to loop through its Fields
collection:

for each fld in rs.Fields
response.write fld.Name & ": " & fld.Value & "<BR>"
next

If you are talking about a "For i=0 to something" loop, then you need to use
a cursortype that supports bookmarks. This is because you need a way to
1. Tell the recordset which record to point to, and
2. Tell the loop to stop at the last record, using the recordcount (which is
only available with static, keyset and dynamic cursors)

Anyways, if you set the cursortype to either 1(keyset), 2(dynamic) or 3
(static), or set the cursorlocation to 3 (adUseClient), guaranteeing that
you will get a static cursor, before you open the recordset, you will
receive a bookmarkable cursor which will allow you to do this:

rows=rs.RecordCount
if rows > 0 then
for i = 1 to rows
rs.AbsolutePosition=i
'do stuff with current record
next
end if

Such cursortypes are more expensive (consume more system resources) than the
default forwardonly cursor. If you are going to loop through a recordset,
then use the simple "Do While Not rs.EOF...Loop" or "Do Until rs.EOF
....Loop" loops - they will be much more efficient. Better yet, use GetRows
or GetString where appropriate.

HTH,
Bob Barrows

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Re: How to loop through a list while inside the loop, the list size may be decreased? Roedy Green Java 3 09-13-2008 01:51 AM
How to loop a recordset and display html with conditions Gabriel Lozano-Morán ASP .Net 2 07-06-2005 09:06 AM
Loop Through a recordset Steven K ASP .Net 1 03-04-2004 01:04 AM
RecordSet.Move or RecordSet.AbsolutePosition?? Hung Huynh ASP General 8 09-24-2003 11:07 AM



Advertisments