I\'m primarily an PHP developer, but I have some old ASP one of our previous developers made that broke and I can\'t figure out how to fix it. We have a program that sends
Common occurrence is likely you do not have adVarChar
defined, so the CreateParameter()
method is "improperly defined".
This is just an example of one of the many named constants found in the ADODB Library. A messy approach to dealing with this is to just define the value yourself something like;
Const adVarChar = 200
The problem with this approach is you then have to define all the named constants which can be a headache. Another approach is to just skip the whole named constants thing and just use the integer values, so the call would be;
.Parameters.Append .CreateParameter("@code", 200, 1, 50, x)
However this isn't easy to read although it looks as though you are doing this already with the ParameterDirectionEnum
value of 1
which is the named constant adParamInput
in the ADODB Library. Regardless I wouldn't recommend this approach.
A slightly better approach is to use an #include
directive so that it includes in the calling page all the named constant definitions you could want and this is how most do it. Microsoft provide a pre defined file for this very purpose with IIS (or possibly the MDAC library installation I'm not sure off the top of my head) known as adovbs.inc
or adovbs.asp
, by including this file your page would have access to all the named constant definitions within.
The reason for all this is VBScript doesn't support type libraries, so in a Client scenario defining them yourself or copying and pasting from the adovbs.inc
file is your only option. However in a Server scenario we still have the power of IIS which lets us do some funky things.
Wouldn't it be nice if the Type Library could just be added once and you don't have to worry about it?, no annoying constants to have to define? Let's face it they already exist in the Type Library so why can't we get them from there? Well turns out we can thanks to the METADATA
directive.
Here is an example;
<!--
METADATA
TYPE="TypeLib"
NAME="Microsoft ActiveX Data Objects 2.5 Library"
UUID="{00000205-0000-0010-8000-00AA006D2EA4}"
VERSION="2.5"
-->
The beauty of this approach is you can use for any Type Library (ideally exposed to COM) and you can define in one page, or add it into the global.asa
to have defined across the entire Web Application.
With this approach you are then safe to use code like;
.Parameters.Append .CreateParameter("@code", adVarChar, adParamInput, 50, x)
In the places where you are defining adVarChar, and other ADO magic numbers, get rid of them completely.
Instead add this to the top of your global.asa (create one if it doesn't) exist.
<!--METADATA TYPE="TypeLib" file="C:\Program Files (x86)\Common Files\System\ado\msado15.dll" -->
This will import the msado15 type library which already has all of those magic numbers defined. It will pull in all the defines for you automatically. Meaning you won't have to define adVarChar etc yourself, the metadata tag will import them automatically for you. Credit to @Lankymart for this as his comment lead me to discovering this and figuring it out, and it works great.
Now, turn create a test asp page that's empty and add some test code to it with option explicit on.
<%
Option Explicit
Response.Write "adVarChar: " & adVarChar
%>
If you get an error that adVarChar is not defined, then msado15.dll is not on the server. So you will need to contact godaddy etc to have them tell you the path to whatever version of MSADO is on there and change your metadata tag to reference the right version.