i am writing parameterized insert query uisng asp but get the following error --------------//error Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.line 34 -------------------- i have table in MSSQL column name (item_name nvarchar 20) asp code ---------- itemname=request.form("itemname") cm.commandtext="insert into test.dbo.item(itemid, itemname) values(231,@item_name)" Set newParameter = cm.CreateParameter("@item_name", nVarChar, adParamInput,20,itemname)//line 34 where it shows error cm.Parameters.Append newParameter -------------- any help will be really appreciated thank you
Have defined the constants: adParamInput and nVarChar?
You don't need to define parameters if you are doing the insert via command text, only if it's a stored procedure. See the code below for both methods of doing this (inline and stored proc): -
'PUT THE CONSTANTS BELOW IN AN INCLUDE FILE CALLED ADOVBS.inc and reference this file from your asp page using the #include directive.
'---- DataTypeEnum Values ----
Const adEmpty = 0
Const adTinyInt = 16
Const adSmallInt = 2
Const adInteger = 3
Const adBigInt = 20
Const adUnsignedTinyInt = 17
Const adUnsignedSmallInt = 18
Const adUnsignedInt = 19
Const adUnsignedBigInt = 21
Const adSingle = 4
Const adDouble = 5
Const adCurrency = 6
Const adDecimal = 14
Const adNumeric = 131
Const adBoolean = 11
Const adError = 10
Const adUserDefined = 132
Const adVariant = 12
Const adIDispatch = 9
Const adIUnknown = 13
Const adGUID = 72
Const adDate = 7
Const adDBDate = 133
Const adDBTime = 134
Const adDBTimeStamp = 135
Const adBSTR = 8
Const adChar = 129
Const adVarChar = 200
Const adLongVarChar = 201
Const adWChar = 130
Const adVarWChar = 202
Const adLongVarWChar = 203
Const adBinary = 128
Const adVarBinary = 204
Const adLongVarBinary = 205
'---- ParameterDirectionEnum Values ----
Const adParamUnknown = &H0000
Const adParamInput = &H0001
Const adParamOutput = &H0002
Const adParamInputOutput = &H0003
Const adParamReturnValue = &H0004
'---- CommandTypeEnum Values ----
Const adCmdUnknown = &H0008
Const adCmdText = &H0001
Const adCmdTable = &H0002
Const adCmdStoredProc = &H0004
'Method 1...
itemname=request.form("itemname")
itemid=231
set cn = server.CreateObject("ADODB.Command")
cn.connectionString = Connection
cn.open
set cmd = server.CreateObject("ADODB.Command")
with cmd
.ActiveConnection = cn
.CommandText = "spAddTestItem"
.CommandType = adCmdStoredProc
.Parameters.Append .CreateParameter("@item_id, adInteger, adParamInput, 4, itemid)
.Parameters.Append .CreateParameter("@item_name, adVarchar, adParamInput, 20, itemname)
.Execute
end with
'Method 2...
function makeSQL(sStr)
sStr = replace(sStr, "'", "''")
makeSQL = sStr
end function
itemname=request.form("itemname")
itemid=231
set cn = server.CreateObject("ADODB.Command")
cn.connectionString = Connection
cn.open
set cmd = server.CreateObject("ADODB.Command")
with cmd
.ActiveConnection = cn
.CommandText = INSERT INTO test.dbo.item(itemid, itemname) values (" & itemid & ", " & makeSQL(itemname) & ")"
.CommandType = adCmdText
.Execute
end with