I'm trying to pass some parameters to a SQL stored procedure in my classic ASP. I've seen several posts on this and not sure what I'm doing wrong as I don't seem to see my discrepancy.
set conn = CreateObject("ADODB.Connection")
conn.open ("DSN=SERVER;UID=username;PWD=pwd;Database=MyDatabase")
set cmd = Server.CreateObject("ADODB.Command")
set cmd.ActiveConnection = conn
cmd.CommandType = adCmdStoredProc
cmd.CommandText = my_proc
cmd.Parameters.Refresh
cmd.Parameters(1) = "MyParam"
set rs = cmd.execute
I'm getting the error
Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.
on the line cmd.CommandType = adCmdStoredProc
. I also tried to do it the following way with the same error
set conn = CreateObject("ADODB.Connection")
conn.open ("DSN=SERVER;UID=username;PWD=pwd;Database=MyDatabase")
set cmd = Server.CreateObject("ADODB.Command")
set cmd.ActiveConnection = conn
cmd.CommandType = adCmdStoredProc
cmd.CommandText = my_proc
cmd.Parameters.Refresh
cmd.Parameters.Append cmd.CreateParameter("@MyParam, adVarWChar, adParamInput, 50, "test")
set rs = cmd.execute
You use ADO through
late binding
, which means that the constants likeadCmdStoredProc
,adParamInput
etc. are unknown to your code (so they are always 0).You can either look them up and define the ones you need as a constant in your code (or use the numbers directly, but that's not well readable if you edit the code again later).
Or take a look here on msdn - you may find a file that defines all the constants at
c:\Program Files\Common Files\System\ado\adovbc.inc
.I think there is only a very small piece that you are doing wrong:
The ADO constants are probably undefined, and also (but not sure) the parameter should be assign via its
value
property.@KekuSemau is correct but let me a suggest a more efficient and manageable approach then using the
adovbs
constants file.METADATA allows you to define a reference to the DLLs constants even if your using Late Binding as is the case in a Classic ASP environment. It's probably worth mentioning that you can add METADATA references in individual pages but then again why would you?
To use it simply add the METADATA tag to your
global.asa
file (should be located in the root of your Web Application).Depending on the system the ADO Type Library maybe different, adjust the
FILE
attribute accordingly.I use this approach in all my applications mainly for referencing constants in the ADO and CDO Type Libraries.
These are examples of my own personal references, file locations maybe different whereas
UUID
attributes should be exactly the same.Useful Links