Today we are discussing common pitfalls to watch out for when developing in ASP.
Microsoft has strived to make their language easy to learn and easy to use but
they have failed to make it "idiot-proof".
Global vs. Local Variables
It is very easy to get confused when using global and local variables in a
script. By "local" here, we mean local to a function or subroutine definition.
A global variable is defined at the script level and may be accessed anywhere in
your script even within a function.
For this reason, it is easy to accidentally use a variable defined at the script-
level within a function without meaning to. In practice it is best to use no
script-level variables within your subroutines.
Another problem with variables is mis-typing a variable name. If you
accidentally type the wrong name for a variable, it will assume the empty value
which corresponds to a zero (0) for numbers, false for boolean and the empty
string.
To avoid this problem you should always use the Option Explicit
directive at the top of your scripts. This can only appear once (for all
included files) and must appear as the first statement in your script.
Working with a Database
When working with a database, there are several problems that you will need to
watch out for. The first of which is the database connection string. This can
be difficult for beginners to understand when they are first starting ASP.
There are many different forms the connection string can take and each one is
specific to the type of database you are using.
For examples of ASP connection strings, we will refer you to this site which
lists a myriad of different connection strings:
ADO Connection String
Examples
Another thing to be careful of is when working with values returned from a
recordset. If the value that is returned in a database field is the database
special value "NULL", it can cause all sorts of problem when used in expressions
in your code. Basically, "NULL" will not automatically convert to anything.
ASP has a special work-around which will allow you to convert all NULLs to an
empty string by using the syntax:
By concatenating an emtpy string to the database field, you will convert null
values to an empty string. If the field has a value, it will be converted to a
string by this expression. Another alternative is to test for null values using
the IsNull function.