ASP Table and EOF - Get error '80020009' - asp-classic

Consider a recordset containing four rows, as below
Team NumDocs
OPS10 2
OPS4 1
OPS5 2
OPS7 3
Consider also the following row in a table to display these.
<td>
<% If Trim(RS("Team")) = "OPS1" And not RS.EOF Then
Response.Write(RS("NumDocs"))
RS.MoveNext
End If%>
</td>
<td>
<% If Trim(RS("Team")) = "OPS10" And not RS.EOF Then
Response.Write(RS("NumDocs"))
RS.MoveNext
End If%>
</td>
<td>
<% If Trim(RS("Team")) = "OPS2" And not RS.EOF Then
Response.Write(RS("NumDocs"))
RS.MoveNext
End If%>
</td>
<td>
<% If Trim(RS("Team")) = "OPS3" And not RS.EOF Then
Response.Write(RS("NumDocs"))
RS.MoveNext
End If%>
</td>
<td>
<% If Trim(RS("Team")) = "OPS4" And not RS.EOF Then
Response.Write(RS("NumDocs"))
RS.MoveNext
End If%>
</td>
<td>
<% If Trim(RS("Team")) = "OPS5" And not RS.EOF Then
Response.Write(RS("NumDocs"))
RS.MoveNext
End If%>
</td>
<td>
<% If Trim(RS("Team")) = "OPS6" And not RS.EOF Then
Response.Write(RS("NumDocs"))
RS.MoveNext
End If%>
</td>
<td>
<% If Trim(RS("Team")) = "OPS7" And not RS.EOF Then
Response.Write(RS("NumDocs"))
RS.MoveNext
End If%>
</td>
<td>
<% If Trim(RS("Team")) = "OPS8" And Not RS.EOF Then
Response.Write(RS("NumDocs"))
RS.MoveNext
End If%>
</td>
<td>
<% If Trim(RS("Team")) = "OPS9" And Not RS.EOF Then
Response.Write(RS("NumDocs"))
RS.MoveNext
End If%>
</td>
The header row contains 10 hard coded headings, OPS1, OPS10, OPS2, etc.
The section above which populates OPS7's colum is fine, and the value of "2" is written to the cell. Then I get an error on the next column for OPS8 - obviously there's no returned value for this as it only goes up to OPS7. This is why I put a Not EOF in the IF statement, but I am still getting the error.
Can anyone assist?

You should move the RS.EOF to the first check in the IF statement E.G., go from this
If Trim(RS("Team")) = "OPS9" And Not RS.EOF Then
to this
If Not RS.EOF Then
If Trim(RS("Team")) = "OPS9" Then
... logic here ...
End If
End If
The problem is that And is not a short circuit operation in classic asp.

Unfortunately there is no error message associated with this code.
80020009 = Accessing data of a recordset that is EOF

Related

XSS Poor Validation Classic ASP

I am getting XSS Poor Validation issue in the following code:
<TABLE cellpadding=0 cellspacing=1 border=0 style="table-layout:fixed">
<col width=20%>
<col width=13%>
<col width=20%>
<col width=13%>
<col width=20%>
<col width=13%>
<TR height=25>
<TD class=border_title_sub colspan=2 align=center>미작성</TD>
<TD class=border_title_sub colspan=2 align=center>임시저장중</TD>
<TD class=border_title_sub colspan=2 align=center>작성완료</TD>
</TR>
<%
do until (rs_1.EOF and rs_2.EOF and rs_3.EOF)
%>
<TR height=25>
<%
if rs_1.EOF then
%>
<TD class=border_text> </TD>
<TD class=border_text align=center> </TD>
<%
else
rs_1_check_name = rs_1("check_name")
rs_1_user_name = rs_1("user_name")
%>
<TD class=border_text><%=HTMLDecode(Server.HTMLEncode(rs_1_check_name))%></TD>
<TD class=border_text align=center><%=HTMLDecode(Server.HTMLEncode(rs_1_user_name))%></TD>
<%
end if
if rs_2.EOF then
%>
<TD class=border_text> </TD>
<TD class=border_text align=center> </TD>
<%
else
rs_2_check_name = rs_2("check_name")
rs_2_user_name = rs_2("user_name")
%>
<TD class=border_text><%=HTMLDecode(Server.HTMLEncode(rs_2_check_name))%></TD>
<TD class=border_text align=center><%=HTMLDecode(Server.HTMLEncode(rs_2_user_name))%></TD>
<%
end if
if rs_3.EOF then
%>
<TD class=border_text> </TD>
<TD class=border_text align=center> </TD>
<%
else
rs_3_check_name = rs_3("check_name")
rs_3_user_name = rs_3("user_name")
%>
<TD class=border_text><%=HTMLDecode(Server.HTMLEncode(rs_3_check_name))%></TD>
<TD class=border_text align=center><%=HTMLDecode(Server.HTMLEncode(rs_3_user_name))%></TD>
<%
end if
%>
</TR>
<%
if not rs_1.EOF then rs_1.movenext
if not rs_2.EOF then rs_2.movenext
if not rs_3.EOF then rs_3.movenext
loop
rs_1.close
rs_2.close
rs_3.close
set rs_1 = Nothing
set rs_2 = Nothing
set rs_3 = Nothing
%>
</TABLE>
Here, HTMLDecode is my custom function, which is defined as follows:
<%
Function HTMLDecode(sText)
Dim I
sText = Replace(sText, """, Chr(34))
sText = Replace(sText, "<" , Chr(60))
sText = Replace(sText, ">" , Chr(62))
sText = Replace(sText, "&" , Chr(38))
sText = Replace(sText, " ", Chr(32))
For I = 1 to 255
sText = Replace(sText, "&#" & I & ";", Chr(I))
Next
HTMLDecode = sText
End Function
%>
As my data already encoded in the DB, I am using both Server.HTMLEncode("") [ To escape from Security SW ) and HTMLDecode("") [To display information properly].
Could you please help me on this.

Download Excel not working properly in ASP

I am new to ASP.I need to write a script for Download Excel in ASP.I tried but it is downloading the entire page content but I need to download the table from database.
Here is my code:
<%#Language="VBScript"%>
<form name="form1" id="form1" method="post">
<input type="hidden" name="action" value="sel">
<table>
<tr>
<td><input type="submit" name="submit" id="submit" value="Download Excel"></td>
</tr>
</table>
Hello World
<%
action = Request.Form("action")
If action="sel" Then
Response.ContentType = "application/octet-stream"
Response.ContentType = "application/vnd.ms-excel"
SET Conn = Server.CreateObject("ADODB.Connection")
Conn.OPEN "PROVIDER=SQLOLEDB;DATA SOURCE=10.1.1.1;UID=sa;PWD=root;DATABASE=Student"
dim Conn,Rs
set Rs=server.createobject("ADODB.recordset")
Rs.open "SELECT * FROM studentdetails",Conn
Response.AddHeader "Content-Disposition", "attachment; filename=xl_data.xls"
%>
<TABLE BORDER=1>
<TR>
<%
j = 2
For i = 0 to RS.Fields.Count - 1
%>
<TD width="18"><B>
<% = RS(i).Name %></B></TD>
<% Next %>
<TD width="42"></TD>
<TD width="53"></TD>
</TR>
<%
Do While Not RS.EOF
%>
<TR>
<% For i = 0 to RS.Fields.Count - 1
%>
<TD VALIGN=TOP><% = RS(i) %></TD>
<% Next %>
</TR>
<%
RS.MoveNext
j = j + 1
Loop
RS.Close
End If
%>
</TABLE>
In this program I have included Hello World line while downloading it is downloading that also.So Please give me some suggestions.THANKS IN ADVANCE.
replace
Response.ContentType = "application/octet-stream"
with
Response.Clear
otherwise you are sending the form and the table to Excel

Inserting SQL data for one table into an html table using ASP.net or Classic ASP

For a bit of background info I'm a Network Admin/Systems Admin with very very minimal self-taught programming 'skills'. In the past I have just modified and tweaked existing code to get whatever I was looking for to work but I'm not having any luck with this current project. I am looking to add dynamic prices from a sql table into an existing html table for only a few of the records that are in the sql table.
I'm using a locally hosted server using IIS6 with a mix of ASP.net 2.0 and classic asp on the site with a 2008 MS SQL Server database.
I already have the connection to the db made in global.asa and am looking for how I can correspond each price to each html item number in the table.
(the sql code I copy/pasted from a different asp file with different intentions so if something looks completely off its probably because of that :[ )
Ex:
<html>
<head>
<%
sql = "SELECT * FROM tblProductCatalogue WHERE ( (tblProductCatalogue.CustomerID = 1 ) and (tblProductCatalogue.ItemNumber = ItemNumber)) "
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open sql, conn, 3, 3
if NOT rs.eof then
rs.MoveFirst
DerivedPrice = rs("DerivedPrice")
rs.close
Set rs = Nothing
%>
</head>
<body>
<table>
<tr>
<th>Item Number</th>
<th>Description</th>
<th>Price</th>
</tr>
<tr>
<td>PartNumber1</td>
<td>description1</td>
<td><%DerivedPrice%></td>
</tr>
<tr>
<td>PartNumber2</td>
<td>description2</td>
<td><%DerivedPrice%></td>
</tr>
<tr>
<td>PartNumber3</td>
<td>description3</td>
<td><%DerivedPrice%></td>
</tr>
</table>
</body>
</html>
Thanks!
Stan
So close! You need a loop around your table rows. Replace your table with this:
if NOT rs.eof then rs.MoveFirst
'...remove this code from your exampl (above), snip---'
DerivedPrice = rs("DerivedPrice")
rs.close 'especially this code'
Set rs = Nothing 'and this code'
'---snip, end----'
%>
<%= rs.RecordCount %> Rows<br/> <!-- optional, for troubleshooting -->
<table>
<tr>
<th>Item Number</th>
<th>Description</th>
<th>Price</th>
</tr>
<%
While NOT rs.eof 'loop through the dataset'
%>
<tr>
<td><%= rs("PartNumber") %></td>
<td><%= rs("Description") %></td>
<td><%= rs("DerivedPrice") %></td>
</tr>
<%
rs.MoveNext
Wend 'end while
rs.close
Set rs = Nothing
%>
</table>

How to add total from a search form

I like to query based on some fields to generate a report: Date range, Department, Student with date range. I have the form to work on searching....
However, I need to calculate totals for each of these fields from a report.
For example: if I search for students and the search results are:
Department Date Range Student Cost
DeptA 1/1/2012-12/31/2012 StuA $100
DeptA 1/1/2012-12/31/2012 StuB $50
DeptA 1/1/2012-12/31/2012 StuC $50
How can I calculate the total of cost automatically online (= $200)?
Thanks.
Here is my code:
<%
Path = Request.ServerVariables("PATH_TRANSLATED")
While (Right(Path, 1) <> "\" And Len(Path) <> 0)
iLen = Len(Path) - 1
Path = Left(Path, iLen)
Wend
Set adoCon = Server.CreateObject("ADODB.Connection")
adoCon.open strCon
SelectStmt = "Select * From view_costs Where "
WhereClause = ""
WhereBetweenClause = "BETWEEN"
If Request("qryDepartment") <> "All Departments" Then
qryDepartment = replace(request("qryDepartment"),"'","''")
WhereClause = WhereClause & "Department = '" & qryDepartment & "' AND "
End If
If Request("qryStudents") <> "All Students" Then
WhereClause = WhereClause & "Name = '" & Request("qryStudents") & "' AND "
End If
sStartDate = Request("StartDate")
sEndDate = Request("EndDate")
If IsDate(sStartDate) And IsDate(sEndDate) Then
WhereClause = WhereClause & "(StartDate >= '" & sStartDate & "' AND EndDate <= '" & sEndDate & "') "
End If
Set adoCon = Server.CreateObject("ADODB.Connection")
adoCon.open strCon
pstart = trim(request.form("pstart"))
pfinish = trim(request.form("pfinish"))
Set getDepartment = Server.CreateObject("ADODB.Recordset")
getDepartment.Open "Select Department from view_costs order by Department;",adoCon
Set getname = Server.CreateObject("ADODB.Recordset")
getname.Open "Select Name from view_costs order by Name;",adoCon
%>
<html>
<head>
<title>The Resport</title>
</head>
<body>
<table width="770" align="center">
<tr>
<td colspan="4" class="n"><span id="h3">Search Menu</span></td>
</tr>
<form method=Department action="Search.asp" name="Search">
<tr>
<td>
<% if not getDepartment.eof then %>
<select name="qryDepartment" size="1" title="Select a Department Name" style="width:200;background-color=#F5D0A9;">
<option selected>All Departments</option>
<% do until getDepartment.eof %>
<option><%= getDepartment("Department") %></option>
<% getDepartment.MoveNext
loop %>
</select>
<% end if %>
</td>
<td>
<% if not getname.eof then %>
<select name="qryStudents" size="1" title="Select a Student Name" style="width:200;background-color=#F5D0A9;">
<option selected>All Students</option>
<% do until getname.eof %>
<option><%= getname("Name") %></option>
<% getname.MoveNext
loop %>
</select>
<% end if %>
</td>
<%
%>
<td><span id="b"> Start:</span> <input name="StartDate" type="text" size="15" maxlength="12" value="<%=sStartDate%>">
<img src="calendar.gif" alt="calendar"></td>
<td><span id="b"> End:</span> <input name="EndDate" type="text" size="15" maxlength="12" value="<%=sEndDate%>">
<img src="calendar.gif" alt="calendar"></td>
</tr>
<tr class="search-bg">
<td colspan="6">
<input type="button" name="Submit" value="Search" onClick="if (isDate()) document.Search.submit();">
</tr></table>
<% If oRs.RecordCount = 0 Then %>
<p></p>
<% Else %>
<table width="960" align="center">
<form method="Department">
<tr bgcolor="#FE9A2E" height="25">
<td class="a"> Department Name</td>
<td class="a"> Student</td>
<td class="a"> Start</td>
<td class="a"> End</td>
<td class="a"> Cost</td>
</tr>
<tr>
<td><%=oRs("Department")%> </td>
<td><%=oRs("Name")%> </td>
<td><%=FormatDateTime(Month(oRs("StartDate")) & "/" & Day(oRs("StartDate")) & "/" & Year(oRs("StartDate")))%> </td>
<td><%=FormatDateTime(Month(oRs("EndDate")) & "/" & Day(oRs("EndDate")) & "/" & Year(oRs("EndDate")))%> </td>
<td <%=sRowStyle%>>$<%=oRs("Cost")%> </td>
</tr>
<% oRs.MoveNext %>
<% WEND %>
</table>
</td>
</tr>
</table>
<% oRs.close
set oRs = nothing
set adoCon = nothing
%>
Why not specify the fields in your SQL (instead of "SELECT * ..."), then add up the values via variables in the loop and output them in a new table row after the loop?
Or you could use SQLs SUM() function to query those values.

Creating a Table at the click of a button with classic asp

I am trying to validate information, without switching the page (in this case a username, if the username is found, great, populate a textbox and dynamically create a table with the username it it). However, I am getting an error on line 75 that reads:
ADODB.Recordset
error '800a0e78'
Operation is not allowed when the object is closed.
/login.asp, line 75
I haven't closed the recordset anywhere. By my knowledge it should work. What am I doing wrong?
<%
Dim cn,rs
Set cn = Server.CreateObject("ADODB.Connection")
Set rs = Server.CreateObject("ADODB.recordset")
cn.Provider = "Microsoft.Jet.OLEDB.4.0"
cn.Open Server.MapPath("login.mdb")
'Default message for the user on the login page
msg = "Please login"
'Logout code. This code empties the session variable which holds the user's userID.
If Request.QueryString("action") = "logout" Then
Session("user_id") = ""
msg = "You have been logged out"
End If
'Check if the form has been submitted
If Request.Form("Submit") = "Test" Then
user_name = Request.Form("user_name")
user_pass = Request.Form("user_pass")
mySQL = "SELECT user_id, user_name, user_pass FROM users WHERE user_name = '" & user_name & "' AND user_pass = '" & user_pass & "'"
'Select the data from the database using the submitted data.
rs.Open mySQL, cn
'Check if a match was found.
If NOT rs.EOF Then
'Session("user_id") = rsLogin("user_id")
'Response.Redirect("profile.asp")
u = rs("user_name")
Else
'If a match was not found then output an error.
Response.Redirect("login.asp?errmsg=Login failed")
End If
End If
%>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form name="form1" id="form1" method="post" action="">
<table width="300" border="0" cellspacing="0" cellpadding="2">
<tr>
<td>Username</td>
<td><input name="user_name" type="text" id="user_name" /></td>
</tr>
<tr>
<td>Password</td>
<td><input name="user_pass" type="password" id="user_pass" /></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Test" /></td>
</tr>
</table>
</form>
<input id="fileno" value="<%=u%>" type="text">
<%
While NOT rs.EOF
%>
<table width="200" border="1" cellspacing="0" cellpadding="2">
<tr>
<th scope="col"><div align="left">username</div></th>
</tr>
<tr>
<td><%=u%></td>
</tr>
<%
rs.MoveNext
Wend
%>
</table>
</body>
</html>
When this condition fails.
If Request.Form("Submit") = "Test"
your code tries to access rs.eof without opening the recordset.
Put the rs.open OUTSIDE of that block.
Means you haven't closed your connection object
try set cn =Nothing at the end of your page

Resources