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]  Subroutine: Query2Table
     [next Lesson]  Subroutine: Highly Reusable

Subroutines Query2List by Charles Carroll

Subroutines can save you having to repeat blocks of code and can optionally be placed in separate files and included when needed, thus making your pages not appear to have lots of code in-line.

   filename=/learn/test/subdblist.asp

<Test Script Below>


<html><head>
<TITLE>subdblist.asp</TITLE>
</head><body bgcolor="#FFFFFF">
<form>
<% 
theDSN="DSN=student;uid=student;pwd=magic"
call query2list("city","publishers","cy",theDSN,"Rockville")
call query2list("state", "publishers","st",theDSN,"MD")
call query2list("zip", "publishers","zp",theDSN,"20851")
%>
</form>
<!--#include virtual="/learn/test/subdblist.inc"-->
</body></html>

The include file lib_dblist.asp looks like:

   filename=/learn/test/lib_dblist.asp

<Test Script Below>


<%sub query2list(myquery,myname,myDSN)
    dim conntemp, rstemp
    set conntemp=server.createobject("adodb.connection")
    conntemp.open myDSN
    set rstemp=conntemp.execute(myquery)
    %>
    <Select name="<%=myname%>">
    <%
    do while not rstemp.eof
        thisfield=trim(RStemp(0))
        if isnull(thisfield) or thisfield="" then
            ' ignore
        else
            response.write "<option>" & thisfield & "</option>"
        end if
        rstemp.movenext
    loop
        %>
    </select>
    <%rstemp.close
    set rstemp=nothing
    conntemp.close
    set conntemp=nothing
end sub%>

Chaz Wish List
Tall Tip $5
Grande Tip $20
Venti Tip $39
Tip Jar Thanks
2004 Thanks
2005 Thanks
HUGE Tip -love site