Can I nest Do loops?

D

Dave

If so, what is wrong with this code in APS 3.0?

I open a recordset with order info. Each record contains the order header
info plus a line item from the order detail. So the recordset looks
something like this:

Orderid PartID
1 12
1 87
2 99
2 33


The web page should display as:

Order 1
Part 12
Part 87

Order 2
Part 99
Part 33


I though I could do this with the following code:


<%
Do while not rs.eof
%>

<tr>
<td>
<%Response.Write (rs.Fields("orderidid") & ".")%>
</td>
</tr>


<%

id=rs.Fields("orderid")

Do while rs.Fields("orderid")=id ' line 119

%>

<tr>
<td>
<%Response.Write (rs.Fields("partid")) %>
</td>
</tr>

<%

rs.movenext

Loop

rs.movenext

Loop


But I get this error:

Error Type:
(0x80020009)
Exception occurred.
/orders/orders.asp, line 119

Line 119 is the second Do Loop


The outer do loop works fine but when I add the inner loop it errors

Can I nest Do Loops in ASP 3.0?

What am I doing wrong here?
 
E

Evertjan.

Dave wrote on 12 nov 2005 in microsoft.public.inetserver.asp.general:
<%
Do while not rs.eof

id=rs.Fields("orderid")

Do while rs.Fields("orderid")=id ' line 119


rs.movenext

The second rs.movenext can give an illegal rs.Fields("orderid") at 119
at the end of the database, I suppose.
rs.movenext

Loop


you could test:

Do until rs.eof
id=rs.Fields("orderid")
Do until rs.Fields("orderid")<>id or rs.eof
'' give output
rs.movenext
Loop
if rs.eof then exitdoed="yes":exit do
rs.movenext
exitdoed="no"
Loop
response.write exitdoed

in the end I would use:

Do until rs.eof
id=rs.Fields("orderid")
Do until rs.eof OR rs.Fields("orderid")<>id
'' give output
rs.movenext
Loop
if not rs.eof then rs.movenext
Loop

NOT TESTED
 
M

MyndPhlyp

Dave said:
If so, what is wrong with this code in APS 3.0?

I open a recordset with order info. Each record contains the order header
info plus a line item from the order detail. So the recordset looks
something like this:

Orderid PartID
1 12
1 87
2 99
2 33


The web page should display as:

Order 1
Part 12
Part 87

Order 2
Part 99
Part 33


I though I could do this with the following code:


<%
Do while not rs.eof
%>

<tr>
<td>
<%Response.Write (rs.Fields("orderidid") & ".")%>
</td>
</tr>


<%

id=rs.Fields("orderid")

Do while rs.Fields("orderid")=id ' line 119

%>

<tr>
<td>
<%Response.Write (rs.Fields("partid")) %>
</td>
</tr>

<%

rs.movenext

Loop

rs.movenext

Loop


But I get this error:

Error Type:
(0x80020009)
Exception occurred.
/orders/orders.asp, line 119

Line 119 is the second Do Loop


The outer do loop works fine but when I add the inner loop it errors

Can I nest Do Loops in ASP 3.0?

What am I doing wrong here?

Yes, you can nest Do loops.

I think you are running into an EOF situation. If you attempt to access a
field's value at EOF an exception will be thrown. You will probably also
encounter an exception on the second MoveNext if EOF was reached by the
first MoveNext. Although not the most graceful of solutions, try the
following:

Do While Not rs.EOF
id = rs.Fields("orderid")
Do While rs.Fields("orderid") = id
rs.MoveNext
If rs.EOF Then
Exit Do
End If
Loop
If Not rs.EOF Then
rs.MoveNext
End If
Loop
 
R

Roland Hall

in message : If so, what is wrong with this code in APS 3.0?
:
: I open a recordset with order info. Each record contains the order header
: info plus a line item from the order detail. So the recordset looks
: something like this:
:
: Orderid PartID
: 1 12
: 1 87
: 2 99
: 2 33
:
:
: The web page should display as:
:
: Order 1
: Part 12
: Part 87
:
: Order 2
: Part 99
: Part 33

This assumes something similar:

strSQL = "SELECT orderid, partid FROM tParts ORDER BY orderid, partid"
set rs = conn.Execute(strSQL)

or a stored query/procedure: (qOrdersParts)
set rs = CreateObject("ADODB.Recordset")
conn.qOrdersParts, rs

dim row, col, order, arr
if not (rs.bof or rs.eof) then
arr = rs.GetRows
prt "<table>"
order = 0
prt "<tr><td>Order " & order & "</td></tr>"
for row = 0 to ubound(arr,2)
if arr(0, row) = order then
prt "<tr><td>Part " & arr(1, row) & "</td></tr>"
else
order = arr(0, row)
prt "<tr><td>Order " & order & "</td></tr>"
prt "<tr><td>Part " & arr(1, row) & "</td></tr>"
end if
next
end if

sub prt(str)
Response.Write str & vbCrLf
end sub

It's untested but looks right at 2:30am. (O:=

--
Roland Hall
/* This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose. */
Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
MSDN Library - http://msdn.microsoft.com/library/default.asp
 
M

MyndPhlyp

Dave:

A slightly more graceful version of my previous reply:

<%
id = Empty
Do Until rs.EOF
If rs.Fields("orderid") <> id Then
id = rs.Fields("orderid")
%>
<tr>
<td>
<%Response.Write (rs.Fields("orderid") & ".")%>
</td>
</tr>
<%
End If
%>
<tr>
<td>
<%Response.Write (rs.Fields("partid")) %>
</td>
</tr>
<%
rs.MoveNext
Loop
%>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top