How This Code Works

Discussion in 'ASP General' started by rn5a@rediffmail.com, Apr 11, 2007.

  1. Guest

    I want an ASP app to retrieve records from a MS-Access database table
    but display them in a HTML table but I want the HTML table table to
    display only 7 records in a row (<tr>...</tr>) in 7 different cells
    (<td>...</td>). In other words, if there are 14 records in the DB
    table, the HTML table should display the 14 records in 2 rows - the
    1st row displaying the 1st 7 records from the DB table & the 2nd row
    displaying the remaining 7 records existing in the DB table. This is
    how I did it:

    ------------------------------------------------
    <%
    On Error Resume Next

    Dim objConn
    objConn=Server.CreateObject("ADODB.CONNECTION")
    'open the connection using ConnectionString

    Dim strSQL
    strSQL="SELECT * FROM MyTable ORDER BY Col1"

    Dim objRS
    Set objRS=Server.CreateObject("ADODB.RECORDSET")
    objRS.Open strSQL,objConn

    Dim iCount
    %>
    <table border=1>
    <%
    Do Until(objRS.EOF)
    %>
    <tr>
    <%
    For iCount=iCount To iCount+6
    %>
    <td><%= objRS("Col1") %></td>
    <%
    objRS.MoveNext
    Next
    %>
    </tr>
    <%
    Loop
    %>
    </table>
    ------------------------------------------------

    The above code does display the records how I want to display but to
    be honest, I couldn't exactly understand the logic of the above code.
    Can someone please explain me the logic behind the above code?

    Also note the On Error Resume Next line. Though the above code
    displays the records exactly how I want them to be displayed in the
    HTML table, if I comment the On Error Resume Next line, then ASP
    generates the

    Exception occured.

    error without pointing to any line. I know what's causing the error
    but can't find a way out to overcome the error. Can someone please
    help me resolve this error as well?

    Thanks.
     
    , Apr 11, 2007
    #1
    1. Advertising

  2. wrote on 11 Apr 2007 14:05:32 -0700:

    > I want an ASP app to retrieve records from a MS-Access database table
    > but display them in a HTML table but I want the HTML table table to
    > display only 7 records in a row (<tr>...</tr>) in 7 different cells
    > (<td>...</td>). In other words, if there are 14 records in the DB
    > table, the HTML table should display the 14 records in 2 rows - the
    > 1st row displaying the 1st 7 records from the DB table & the 2nd row
    > displaying the remaining 7 records existing in the DB table. This is
    > how I did it:
    >
    > ------------------------------------------------
    > <%
    > On Error Resume Next
    >
    > Dim objConn
    > objConn=Server.CreateObject("ADODB.CONNECTION")
    > 'open the connection using ConnectionString
    >
    > Dim strSQL
    > strSQL="SELECT * FROM MyTable ORDER BY Col1"
    >
    > Dim objRS
    > Set objRS=Server.CreateObject("ADODB.RECORDSET")
    > objRS.Open strSQL,objConn
    >
    > Dim iCount
    > %>
    > <table border=1>
    > <%
    > Do Until(objRS.EOF)
    > %>
    > <tr>
    > <%
    > For iCount=iCount To iCount+6
    > %>
    > <td><%= objRS("Col1") %></td>
    > <%
    > objRS.MoveNext
    > Next
    > %>
    > </tr>
    > <%
    > Loop
    > %>
    > </table>
    > ------------------------------------------------
    >
    > The above code does display the records how I want to display but to
    > be honest, I couldn't exactly understand the logic of the above code.
    > Can someone please explain me the logic behind the above code?


    It's pretty simple - get the results, and for every 7 rows in the recordset
    write them out in TD tags. First time round the For Next loop iCount will
    increment from 0 to 6, second time round from 7 to 13, etc. although the
    value of iCount is meaningless - it could just as easily have been "For
    iCount = 1 To 7" and it would do the same job.


    > Also note the On Error Resume Next line. Though the above code
    > displays the records exactly how I want them to be displayed in the
    > HTML table, if I comment the On Error Resume Next line, then ASP
    > generates the
    >
    > Exception occured.
    >
    > error without pointing to any line. I know what's causing the error
    > but can't find a way out to overcome the error. Can someone please
    > help me resolve this error as well?


    If the number of rows in the result is less than a multiplication of 7 (ie.
    7, 14, 21, etc) then the MoveNext will cause an error because there is no
    checking for EOF in the For Next loop. The On Error Resume Next disguises
    this, and allows the creation of empty cells by simply ignoring the error
    caused by = objRS("Col1") when the recordset has passed the last row, and
    subsequently by the MoveNext because the recordset is already past the last
    row so there is no row to move to. The following edit should handle this
    without the need for the On Error statement

    <%
    Dim objConn
    objConn=Server.CreateObject("ADODB.CONNECTION")
    'open the connection using ConnectionString

    Dim strSQL
    strSQL="SELECT * FROM MyTable ORDER BY Col1"

    Dim objRS
    Set objRS=Server.CreateObject("ADODB.RECORDSET")
    objRS.Open strSQL,objConn

    Dim iCount
    %>
    <table border=1>
    <%
    Do Until(objRS.EOF)
    %>
    <tr>
    <%
    For iCount=iCount To iCount+6
    %>
    <td>
    <%
    If Not objRS.EOF Then
    Response.Write objRS("Col1")
    objRS.MoveNext
    End If
    %>
    </td>
    <%
    Next
    %>
    </tr>
    <%
    Loop

    %>
    </table>



    Dan
     
    Daniel Crichton, Apr 12, 2007
    #2
    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. dbuchanan
    Replies:
    9
    Views:
    4,168
    Jim Wooley
    Feb 8, 2006
  2. Johann Blake
    Replies:
    1
    Views:
    1,209
    Hermit Dave
    Jan 5, 2004
  3. F. GEIGER
    Replies:
    3
    Views:
    776
    F. GEIGER
    Aug 6, 2004
  4. Alexander Burger

    getMethod() works and works not

    Alexander Burger, Nov 27, 2010, in forum: Java
    Replies:
    25
    Views:
    1,773
    Alexander Burger
    Nov 29, 2010
  5. abargaddon
    Replies:
    1
    Views:
    199
    clintmazur
    Feb 4, 2008
Loading...

Share This Page