Try It Out - Table Using EOF - Page 10
November 27, 2000
In this example we'll build a table that lists the first and last
names of all of the sailors in the People table of the
sailors database.
<%
dim oRSeof
set oRSeof=Server.createObject("ADODB.recordset")
oRSEOF.Open "PEople", "DSN=sailors"
oRSeof.MoveFirst
Response.Write "<TABLE BORDER='1'>"
Do while NOT oRSeof.EOF
Response.Write "<TR><TD>" & oRSeof("PeopleNameFirst") & "</TD>"
Response.Write "<TD>" & oRSeof("PeopleNameLast") & "</TD></TR>"
oRSeof.MoveNext
Loop
Response.Write "</TABLE>"
%>
How It Works - Table Building with EOF
The first few lines create the record set.
<%
dim oRSeof
set oRSeof=Server.createObject("ADODB.recordset")
oRSEOF.Open "People", "DSN=sailors"
Then with the following line we ensure that we are on the first
record, followed by the tag to start the table.
oRSeof.MoveFirst
Response.Write "<TABLE BORDER='1'>"
Now we begin the loop. We want to loop if the opposite
(NOT) of rs.EOF is true. In other words
when EOF is False (not yet at end of
records, or in other words not yet beyond the last record) we
want ASP-ADO to see that as true and do another loop. When
EOF is True (we are now done with the
records, or in other words, beyond the last record) then we want
ASP-ADO to see that as false and stop cycling.
Do while NOT oRSeof.EOF
Within each cycle we will build a row. That means start with the
<TR> tag. Then add three items for each cell:
<TD>, data and </TD>. At
the end of the row we put in a </TR>.
Response.Write "<TR><TD>" & oRSeof("PeopleNameFirst") & "</TD>"
Response.Write "<TD>" & oRSeof("PeopleNameLast") & "</TD></TR>"
Without the next line ASP-ADO will cycle forever, writing more
rows of the information for sailor number one. We must instruct
your code to move down to the next sailor record after building
the table row.
oRSeof.MoveNext
Will Bad Loops Cycle Forever?
We've all done it; forget to put in the .MoveNext
and run the page, causing an infinite loop. The server is working
away, perhaps putting up thousands of duplicate lines and we
suddenly realize our mistake. At this point you have several
options. First, understand that ASP scripts time out after about
2 minutes. If you are running a page on a remote host you can
stop your browser, correct the problem and then revisit the page.
If you are running PWS you can speed things up by
Start/Programs/MS Personal Web Server/Personal Web Manager/Stop.
Then start it again.
That is it for the loop, and we write the table closing tag to the page.
Loop
Response.Write "</TABLE>"
%>
Reading All of the Records (with EOF) - Page 9
Beginning ASP Databases
Try It Out - Table Using EOF and a Counter - Page 11
|