Displaying Table Data with Classic ASP

This page demonstrates the capabilities how to display a table from a SQL statement.

 

<html><head>
<TITLE>dbsimple.asp</TITLE>
</head>
<body bgcolor=”#FFFFFF”>
<%
 this code opens the database
myDSN=”DSN=Student;uid=student;pwd=magic”
set conntemp=server.createobject(“adodb.connection”)
conntemp.open myDSN

 this code retrieves the data
mySQL=”select * from publishers where state=NY
set rstemp=conntemp.execute(mySQL)

 this code detects if data is empty
If  rstemp.eof then
response.write “No records matched<br>”
response.write mySQL & “<br>So cannot make table…”
connection.close
set connection=nothing
response.end
end if
%>
<table border=1>
<%
 This code puts fieldnames into column headings
response.write “<tr>”
for each whatever in rstemp.fields
response.write “<td><B>” & whatever.name & “</B></TD>”
next
response.write “</tr>”

 Now lets grab all the records
DO  UNTIL rstemp.eof
 put fields into variables
pubid=rstemp(“pubid”)
name=rstemp(“name”)
company_name=rstemp(“company name”)
address=rstemp(“address”)
city=rstemp(“city”)
state=rstemp(“state”)
zip=rstemp(“zip”)
telephone=rstemp(“telephone”)
fax=rstemp(“fax”)
comments=rstemp(“comments”)

 write the fields to browser
cellstart=”<td align=””top””>”
response.write “<tr>”
response.write cellstart & pubid & “</td>”
response.write cellstart & name & “</td>”
response.write cellstart & company_name & “</td>”
response.write cellstart & address & “</td>”
response.write cellstart & city & “</td>”
response.write cellstart & state & “</td>”
response.write cellstart & zip & “</td>”
response.write cellstart & telephone & “</td>”
response.write cellstart & fax & “</td>”
response.write cellstart & comments & “</td>”

response.write “</tr>”
rstemp.movenext
LOOP
%>
</table>

<%
 Now close and dispose of resources
rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing
%>

</body></html>

Advertisements