|
Display Table on Web
Page by Charles Carroll
This page demonstrates the capabilities how to
display a table from a SQL statement. It illustrates not only how to display the table,
but also how to detect that no records were returned from a query, and how to detect null
and blank values in the data.
filename=/learn/test/dbtable.asp
<html><head>
<TITLE>dbtable.asp</TITLE>
</head>
<body bgcolor="#FFFFFF">
<%
' ASP program that displays a database in table form
myDSN="DSN=Student;uid=student;pwd=magic"
mySQL="select * from publishers where state='NY'"
showblank=" "
shownull="-null-"
set conntemp=server.createobject("adodb.connection")
conntemp.open myDSN
set rstemp=conntemp.execute(mySQL)
If rstemp.eof then
response.write "No records matched<br>"
response.write mySQL & "<br>So cannot make table..."
conntemp.close
set conntemp=nothing
response.end
end if
%>
<table border=1><tr>
<% 'Put Headings On The Table of Field Names
for each whatever in rstemp.fields%>
<td><b><%=whatever.name%></B></TD>
<% next %>
</tr>
<% ' Now lets grab all the records
DO UNTIL rstemp.eof %>
<tr>
<% for each whatever in rstemp.fields
thisfield=whatever.value
if isnull(thisfield) then
thisfield=shownull
end if
if trim(thisfield)="" then
thisfield=showblank
end if%>
<td valign=top><%=thisfield%></td>
<% next %>
</tr>
<%rstemp.movenext
LOOP%>
</table>
<%
rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing
%>
</body></html>
|