Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > Can I nest Do loops?

Reply
Thread Tools

Can I nest Do loops?

 
 
Dave
Guest
Posts: n/a
 
      11-12-2005
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?


 
Reply With Quote
 
 
 
 
Evertjan.
Guest
Posts: n/a
 
      11-12-2005
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


> Loop


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

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

 
Reply With Quote
 
 
 
 
MyndPhlyp
Guest
Posts: n/a
 
      11-12-2005

"Dave" <> wrote in message
news:...
> 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


 
Reply With Quote
 
Roland Hall
Guest
Posts: n/a
 
      11-13-2005
"Dave" wrote in message news:...
: 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


 
Reply With Quote
 
MyndPhlyp
Guest
Posts: n/a
 
      11-13-2005
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
%>


 
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
How can I nest variadic functions? Boltar C Programming 2 09-17-2007 10:56 PM
Can't nest popups...WTF? The Natural Philosopher Javascript 2 12-11-2006 07:53 PM
Can't Nest Tables in Excel Adrienne HTML 12 02-25-2005 11:01 PM
Nest controls in a MS Treeview Web Control Chad ASP .Net 1 12-18-2003 05:00 PM
Can I nest a JEditorPane inside a JScrollPane inside a JPanel? Mike Java 5 12-02-2003 01:45 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57