Doug Chamberlain
Doug Chamberlain

Reputation: 11341

<Object> tag instead of Server.CreateObject in ASP Classic negates the use of set variable = nothing?

SO, I learned that in old school ASP Classic if you use the

<object runat="server" id="somename" progid="ADODB.Recordset"></object>

tag it provides a performance improvement over using

set rs = Server.CreateObject("ADODB.Recordset")

However, all was lost when I tried to execute. set rs = nothing Is this the intended behavior? does that tag make asp handle disposing of object better?

Upvotes: 3

Views: 1238

Answers (1)

GWR
GWR

Reputation: 2008

If you are including this object declaration directly in your asp page (not global.asax) then it terminates itself once the page request ends.

You can also include these in your global.asa file if you want them to persist over the session or application scope.

<OBJECT ID=rsCustomers PROGID="ADODB.Recordset" RUNAT="Server" SCOPE="Application"></OBJECT>

Note the SCOPE attribute. This loads the recordset into an application variable (can be set to either "session" or "application" depending on your use case). You can easily destroy session variables in your scripts like this:

Session.Abandon

Or if you want to kill the session variable but leave the session open, just terminate the session variable like this:

Set Session("rsCustomers") = Null

If you just want the RS object for the page, then leave out the scope attribute, and include the object declaration in the page itself.

The id will be available as a regular variable as though you declared it in a vbscript codeblock directly in the page. Just remember to do a .close to close the object, and the object will terminate on its own as soon as the page load is complete (e.g. when the page scope is complete).

Hope this helps.

Upvotes: 1

Related Questions