Age-old ASP Tips

May 19, 2004

Basic, but commonly ignored, these concepts are worth mentioning. The goal is to streamline the application with efficient code. Keep these tips in mind, and you are on your way to creating highly flexible applications. And, just a note, the focus here is ASP 4 & 5.

ASP Uses Only VARIANT Data Types
When calling a COM component, ASP will convert the variant to the ByVal parameters of type int, long, string, etc. automatically.  A ByRef type parameter however, MUST BE A VARIANT.  This means if you wish to return a value or an array through a parameter it must be of type VARIANT.  An array, therefore, becomes a variant containing an array. NOT a variant Array (or array of variants).  Note that function return values can be of any type, it’s only the parameters that must follow this rule. For example, don’t use Dim myValue(11). Instead, first Dim myValue then Redim myValue(11).

Use #INCLUDE to Reuse Code
The #include feature is extremely handy, and is the only way to reuse code within ASP.  It’s a good idea to create a file library of commonly used routines in a central web location.  Name the files according to the type of routines in it.  For instance, debug.asp file has a number of routines for dumping the contents of the request, application, and session objects.  This is great for finding the state of the web request.  To use, simply insert <!– #include virutal = “/include/functions/debug.asp” –> at the beginning of the ASP page. Note that if you are working on another server you will need to copy these files to that server. The “virtual” reference will only work with files on the same server.

Close Objects When Done Using Them
Be sure to close and or release your objects and database connections as soon as they are no longer needed within an ASP page.  Even though the current version of IIS doesn’t actually release the objects until the page is finished, it is still good programming practice.

Do Not Use Server.CreateObject When Instantiating Objects
Unless your component makes use of the IIS Request, Response, Session, or Application objects you do not need to use the server.createobject method to create an object.  It’s faster to use the system CreateObject.  Server.Createobject performs additional overhead to marshal the object references to the component.  This is especially true if the component is on another server and accessed using DCOM (all that marshalling has to occur across the network).   And, if the marshalling needs to go through a Firewall, forget it. It will prevent the object from being created (see Microsoft Knowledge Base Article – 193230).  Server.Createobject is not necessary for use with MTS.  Any component registered under MTS will run under MTS regardless of how or where it is instantiated. You may see documentation elsewhere that seems to contradict this, but I heard it directly from Microsoft.

Gather the Database Connection String from the Registry
Store the connection string for any SQL Server database in the registry and read into an application variable when the application starts using Application(“sConnectString”) – or something similar.  This allows any server you may be working with to automatically load the correct connection string to point at the correct database server without modifying any code.  If you are writing a component for use with ASP, I highly suggest utilizing this method.  Connection strings should NEVER be hard coded into a component, as it becomes very difficult to move the component between environments.  The connection string should be passed to each method call (remember statelessness).

Base Targets Are Out
Using inside the section WILL cause any JavaScript you have inside an href on the page to fail!  For example, href=”javascript:DoSomething(); will fail if there is a base target specified in in the HTML header of the page.

#Include in Global.asa
It is possible to use the #include feature within a Global.asa file, but with one restriction. Blocks of code must be surrounded with
block of code
< script runat=… server language=… >block of codetags.

Facebooktwittermail