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]  advice: Named constants for ADO are better
     [next Lesson]  advice: Server.MapPath is Good

ASP Commandment #5: Clean Up Objects

Close each  object when done with them. This frees up the "handle" to the resource. This applies to objects that support close methods, especially connections and recordsets!

Set each object variable to nothing when done with it.
this frees up the memory devoted to the resource and/or returns it to the resource pool.

The code sample below makes a list box from a database. Notice the close and set to nothing at the end of the code. NEVER forget it in your code.

   filename=/learn/test/dblist.asp

<Test Script Below>


<html><head>
<TITLE>dblist.asp</TITLE>
</head><body bgcolor="#FFFFFF">
<% 
myDSN="DSN=Student;uid=student;pwd=magic"
mySQL="select author from authors where AU_ID<100"

' displays a database field as a listbox
set conntemp=server.createobject("adodb.connection")
conntemp.open myDSN
set rstemp=conntemp.execute(mySQL)
if rstemp.eof then
    response.write "no data for<br>"
    response.write mySQL
    conntemp.close
    set conntemp=nothing
    response.end        
end if

%>
<form action="dblistrespond.asp" method="post">
<Select name="authorname">
<%
' Now lets grab all the data
do until rstemp.eof %>
    <option> <%=RStemp(0)%> </option>
    <%
    rstemp.movenext
loop

rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing
%>
<input type="submit" value="Choose Author">
</Select></form>
</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.