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]  Getrows to display database table
     [next Lesson]  Disconnected Recordsets, Display Table

GetRows NOT BY THE Numbers by Charles Carroll

Getrows Rocks! It is lightning fast and much more scalable than the .MOVENEXT approach. See a complete explanation @
http://www.learnasp.com/advice/whygetrows.asp

Tired of accessing a getrows array by number? There is an easier way we show here below*. But below is the most straightforward ways to use names instead of numbers with a Getrows array.

* and an easier ENCAPSULATED way (easy to use, complex source code) at:
http://www.learnasp.com/freebook/asp/getrowsultimate.aspx

   filename=/learn/test/dbtablegetrowsnonum.asp

<Test Script Below>


<%@enablesessionstate=false%>
<%response.buffer=true%>
<html><head>
<TITLE>dbtablegetrowsnonum.asp</TITLE>
</head>
<body bgcolor="#FFFFFF">
<% 
' displays a database in table form via GetRows
myDSN="DSN=Student;uid=student;pwd=magic"
'mySQL="select PubID, [Company Name], Address, City, State, Zip, Telephone, Fax, Comments "
' above statement fails because of [company name] still researching why....
mySQL="select * "
mySQL=mySQL & " from publishers"  
mySQL=mySQL & " where state='NY'"
set conntemp=server.createobject("adodb.connection")
conntemp.open myDSN
set rstemp=conntemp.execute(mySQL)
response.write rstemp(2).name

If rstemp.eof then
    response.write "No records matched<br>"
    response.write mySQL & "<br>So cannot make table..."
    Call CloseAll
    response.end
end if

response.write "<table border='1'><tr>" & vbcrlf
'Put Headings On The Table of Field Names
for each whatever in rstemp.fields
    response.write "<td><b>" & whatever.name & "</B></TD>" & vbcrlf
next
response.write "</tr>" & vbcrlf

' Now lets grab all the records
alldata=rstemp.getrows
Call CloseAll

numrows=ubound(alldata,2)
fld_pubid=0
fld_Name=1
fld_companyname=2
fld_address=3
fld_city=4
fld_state=5
fld_zip=6
fld_telephone=7
fld_fax=8
fld_comments=9

FOR rowcounter= 0 TO numrows
    response.write "<tr>" & vbcrlf
    pubid=alldata(fld_pubid,rowcounter)
    name=alldata(fld_name,rowcounter)
    companyname=alldata(fld_companyname,rowcounter)
    address=alldata(fld_address,rowcounter)
    city=alldata(fld_city,rowcounter)
    state=alldata(fld_state,rowcounter)
    zip=alldata(fld_zip,rowcounter)
    telephone=alldata(fld_telephone,rowcounter)
    fax=alldata(fld_fax,rowcounter)
    comments=alldata(fld_comments,rowcounter)

    pubid=cleanfield(pubid)
    response.write "<td valign=top>" & pubid & "</td>" & vbcrlf    

    name=cleanfield(name)
    response.write "<td valign=top>" & name & "</td>" & vbcrlf    

    companyname=cleanfield(companyname)
    response.write "<td valign=top>" & companyname & "</td>" & vbcrlf    

    
    companyname=cleanfield(address)
    response.write "<td valign=top>" & address & "</td>" & vbcrlf    

    city=cleanfield(city)
    response.write "<td valign=top>" & city & "</td>" & vbcrlf    

    state=cleanfield(state)
    response.write "<td valign=top>" & state & "</td>" & vbcrlf    

    zip=cleanfield(zip)
    response.write "<td valign=top>" & zip & "</td>" & vbcrlf    

    telephone=cleanfield(telephone)
    response.write "<td valign=top>" & telephone & "</td>" & vbcrlf    

    fax=cleanfield(fax)
    response.write "<td valign=top>" & fax & "</td>" & vbcrlf    

    comments=cleanfield(comments)
    response.write "<td valign=top>" & comments & "</td>" & vbcrlf    
    
    response.write "</tr>" & vbcrlf
NEXT
response.write "</table>" 
%>
</body></html>
<%
SUB CloseAll
    rstemp.close
    set rstemp=nothing
    conntemp.close
    set conntemp=nothing
END SUB

FUNCTION CleanField(parm1)
        cleanfield=parm1
        if isnull(parm1) then
            cleanfield="&nbsp;"
        end if
        if trim(parm1)="" then
            cleanfield="&nbsp;"
        end if
END FUNCTION
%>

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.