Search Search

#1 worldwide
FREE Coding Lessons

since 1996
   THE BEST WAY to learn ASP & Asp.net!
Advertise Here!
click for details
Credits Host:
DiscountASP.net
Server Admin:
The "Team"
Contact Info.
Charles M. Carroll

my Blog
[prev. Lesson]  SQL Mistakes Everyone Makes
     [next Lesson]  Getstring to display database table

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

<Test Script Below>


<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="&nbsp;"
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>

There are many worthy charities!!. But perhaps help starving children in Africa or South America AND help Charles too. a $5 tip buys him lunch at McDonalds, a $20 tip buys his kid Hitoshi a new computer game, a $39 tip buys his daughter Michiko a few nice outfits. See our donor list.