Classic Asp Do While Loop Showing Error
I have a classic ASP page with a simple html table, and I want to loop the table rows based on a unknown number of records pulled from the database, however, when I am looping the
Solution 1:
I know I am rusty on this one but try this:
<%
Dim oddFlag
oddFlag = 1dowhilenot rsTest.eof
if oddFlag=1 Then
oddFlag=0
Response.write("<tr class='odd'>")
Response.write("<td colspan='5'>")
Response.write(rsTest.Fields.Item("field").Value)
Response.write("</td></tr>")
else
oddFlag=1
Response.write("<tr class='even'>")
Response.write("<td colspan='5'>")
Response.write(rsTest.Fields.Item("field").Value)
Response.write("</td></tr>")
endif
rsTest.moveNext
loop
%>
Solution 2:
Since the other answers don't mention this: the problem with your code is that you're doing MoveNext twice, and the second one doesn't test if the first one already reached the EOF.
In any case, that's a needlessly complicated way to do alternating colors.
dim i, rs
'... database stuff, table header, etc.
i = 0
Do Until rs.EOF
i = i + 1
Response.Write "<tr class='"
If i Mod 2 = 0 Then Response.Write "even" Else Response.Write "odd" End If
Response.Write "'>"
'... write out the actual content of the table
Response.Write "</tr>"
rs.Movenext
Loop
'... clean up database, close table
With this method, your counter variable (i) is available as an actual, well, counter - so for example if you want to write out a "number of rows returned" message at the end, you can.
Solution 3:
Little bit sloppy here but this is how I would normally accomplish this:
<%
Dim i
i = 1dowhilenot rsTest.eof
If i = 1Then %>
<tr class="odd">
<% Else %>
<tr class="even">
<% EndIf %>
<td colspan="5"><%=(rsTest.Fields.Item("field").Value)%></td>
</tr>
<%
i = i + 1If i = 3Then i = 1
count = count + 1
rsTest.moveNext
loop %>
Solution 4:
Why not just use:
While not rs.EOF
'stuff
rs.movenext
wend
Or to make sure:
if not rs.eof then
while not rs.eof
'stuff
rs.movenext
wend
end ifAnd even better way is to cache everyting and keep connection very short:
'... set global base (include file)dim dbcon, rs, rsSQL, rsArray
Function openCon()
set dbcon = server.createobject("ADODB.Connection")
dbcon.open Application("YOURDB_Connectionstring")
EndFunctionFunction closeCon()
dbcon.Close
set dbcon = nothingEndFunctionfunction rw(stringwriteshortcut)
response.write(stringwriteshortcut)
endfunction'... end global'... Database interaction:
rsSQL = "SELECT item1, item2 FROM table where ID = 1"
openCon()
set rs = dbcon.execute(rsSQL)
ifnot rs.eof then
rsArray = rs.getRows();
endif
closeCon()
dim items
if isarray(rsArray) thenfor items = 0to UBound(rsArray, 2)
rw(rsArray(0,items) &"<br>")
rw(rsArray(1,items) &"<br>")
nextelse
rw("nothing there")
endif
Post a Comment for "Classic Asp Do While Loop Showing Error"