ASP VBScript for PHP programmers

I wrote this little manual for those PHP programmers who as by some unlucky reason are forced to write something in ASP's VBScript. And yes, you have to be forced to do that because I think VBScript is a real pain in the ass to use.

So, I put up a simple list of suggestions and solutions for problems I had to learn and overcome when I was migrating my already working CMS system from PHP to ASP (don't ask me why ok? just follow me: "management department sucks")

So.. that ASP VBScript is:

  • it's a scripting language for MS IIS server
  • it is highly object-oriented language
  • it sucks because most of object are half-baked stuff (reverse array, anyone?)
  • it sucks in easy-of-use and implementation department comparing to PHP (and feels like a language from stone age)
  • a language where databases access classes are actually not bad at all (one you learn how to use them)
  • an annoying language where you have to define every single variable you use in advance (via "Dim myvar") and of course I know that defining vars is a good style and all, but I don't think it is really necessary when your "program" lives less than a second and then all its data gets destroyed

A bit about programming problems and solutions.

File upload handling

you are definitely better off with some component solution. Pure-ASP solutions "might" work for you if you are living in a single-byte web world (didn't work for my Japanese 2-byte encoded data). The best solution I have found (and free!) was: BASP21
It's a little bit in Japanese but just download, install and search for functions list at the bottom of the page - it's pretty clear how to use it

There are no dynamic arrays

yes. nothing like $myarray[] = "new element". Also, you can't sort, reverse and almost do anything useful with VBScript array. But there are solutions

Multiple-lines operators

as lines in VBScript are not ended with ";" like in PHP, it's not easy to split long strings for several lines. The solution is to use the "_" operator. Have a look below:

Dim myLongArray
myLongArray = Array("ok here we go", _
"next element goes here", _
"you got the idea")

Dim myLongString
myLongString = "Some strings can actually " &_
"be pretty long and boring and span " &_
"multiple lines"

There's no "escape" :)
You escape differently in VBScript. Instead of using the backslash construct to escape quotes (like \"), you just duplicate quotes (like "")

 

Database connection
I have MS SQL Server 2005 handling connections from my ASP scripts, and here goes a snippet of the simplest possible connection scheme I could think of :)
Prerequisites: you have to setup a DSN to connect to the server first.
'defining constants
Const CONST_DSN_NAME = "cms"
Const CONST_DB_USER = "sa"
Const CONST_DB_PASS = ""
Dim objDB
connString = "DSN="&CONST_DSN_NAME&";UID="&CONST_DB_USER&";PASSWORD="&CONST_DB_PASS
'creating database connection
Set objDB = Server.CreateObject("ADODB.Connection")
objDB.connectionString = connString
objDB.Open
'making a simple query
Dim qstring
qstring = "select * from mytable"
set rs = Server.CreateObject("ADODB.Recordset")
rs.Open qstring, objDB, adOpenStatic
'print result
Response.Write "data:" & rs("field_name")

getenv("DOCUMENT_ROOT")?
You might wonder if there is an analog for this function in ASP? There is an analog. Just do this:

Dim docRoot
docRoot = Server.MapPath("/")

SET your object vars
Yeah I bloody know that different syntax for defining a normal variable and variable which actually contains class is not very convenient, but VBScript does just that. So.. if you want to store just a string or a number, you write


Dim myvar
myvar = "easy breezy"

But if you want to store an object in the variable (a Recordset or an Array for example), do this:

Dim myvar
set myvar = new Server.CreateObject("ADODB.Recordset")

Inconvenient. I know.