Do..While..Loops in ASP

Do Loop Part #1  by Charles Carroll

To execute a code sequence more than once ASP provides:
* DO, LOOP
* WHILE, WEND
Either of these statements can be followed by UNTIL or WHILE.

DO UNTIL
…..code to be repeated…
LOOP

DO
…..code to be repeated…
LOOP UNTIL

Do Loop and Timeouts by Charles Carroll

A loop that is infinite will not run forever. IIS will timeout the script (default is 90 seconds).

Here is an infinite loop that IIS will timeout:

<%response.buffer=true%>
<TITLE>doloop1.asp</TITLE>
<body bgcolor="#FFFFFF">
<HTML>
<%
DO
counter=counter+1
response.write counter & "<br>"
response.flush
LOOP
%>
</BODY>
</HTML>

Here is an infinite loop that we explicitly set a timeout for:

<%
response.buffer=true
server.scripttimeout=20
%>
<TITLE>loop2.asp</TITLE>
<body bgcolor="#FFFFFF">
<HTML>
<%
DO
counter=counter+1
response.write counter & "<br>"
response.flush
LOOP
%>
</BODY>
</HTML>

It has been assumed that a timed out script was impossible to intercept, but the next lesson shows how to use the transactional aspect of an ASP script to capture this elusive condition.

Do Loop Intercept Timeouts by Charles Carroll

The transactional nature of ASP pages can be used to intercept a script timeout.
loop3.asp traps a timeout:

<%@ TRANSACTION=Required%>
<%
response.buffer=true
server.scripttimeout=20
%>
<HTML>
<TITLE>loop3.asp</TITLE>
<body bgcolor="#FFFFFF">
</BODY>
<%
DO
counter=counter+1
response.write counter & "<br>"
LOOP
response.flush
response.write "Script executed without incident"
%>
</HTML>
<%
Sub OnTransactionAbort()
response.clear
Response.Write "The Script Timed Out"
end sub
%>

loop4.asp succeeds and does not trigger the trap:

<%@ TRANSACTION=Required%>
<%
response.buffer=true
server.scripttimeout=40
%>
<HTML>
<TITLE>loop4.asp</TITLE>
<body bgcolor="#FFFFFF">
</BODY>
<%
DO  UNTIL counter=400
counter=counter+1
response.write counter & "<br>"
LOOP
response.flush
response.write "Script Exexuted without incident!"
%>
</HTML>
<%
Sub OnTransactionAbort()
response.clear
Response.Write "The Script Timed Out"
end sub
%>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s