Can I nest Do loops?

Discussion in 'ASP General' started by Dave, Nov 12, 2005.

  1. Dave

    Dave Guest

    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?
    Dave, Nov 12, 2005
    #1
    1. Advertising

  2. Dave

    Evertjan. Guest

    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)
    Evertjan., Nov 12, 2005
    #2
    1. Advertising

  3. Dave

    MyndPhlyp Guest

    "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
    MyndPhlyp, Nov 12, 2005
    #3
  4. Dave

    Roland Hall Guest

    "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
    Roland Hall, Nov 13, 2005
    #4
  5. Dave

    MyndPhlyp Guest

    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
    %>
    MyndPhlyp, Nov 13, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike
    Replies:
    5
    Views:
    4,991
  2. Adrienne

    Can't Nest Tables in Excel

    Adrienne, Feb 25, 2005, in forum: HTML
    Replies:
    12
    Views:
    8,925
    Steve Pugh
    Feb 25, 2005
  3. Boltar

    How can I nest variadic functions?

    Boltar, Sep 17, 2007, in forum: C Programming
    Replies:
    2
    Views:
    337
    Keith Thompson
    Sep 17, 2007
  4. Me
    Replies:
    2
    Views:
    226
  5. The Natural Philosopher

    Can't nest popups...WTF?

    The Natural Philosopher, Dec 11, 2006, in forum: Javascript
    Replies:
    2
    Views:
    96
    The Natural Philosopher
    Dec 11, 2006
Loading...

Share This Page