Bad variable type exception

G

Guest

I have made this function to create a command object to be used to call a
Stored Procedure:

Public Overrides Function GetStoredProcedureCommandObject(ByVal cn As
ADODB.Connection) As ADODB.Command
'* Create a command objekt to call SP
Dim cmd As New ADODB.Command()
Dim prm As ADODB.Parameter
cmd.ActiveConnection = cn
cmd.CommandType = ADODB.CommandTypeEnum.adCmdStoredProc
cmd.CommandTimeout = 15
cmd.CommandText = "LEVE_Lev"
prm = cmd.CreateParameter("nLev", ADODB.DataTypeEnum.adInteger,
ADODB.ParameterDirectionEnum.adParamInput)
MsgBox("Me.Lev=" & Me.Lev)
prm.Value = Me.Lev
cmd.Parameters.Append(prm)
Return cmd
End Function

The parameter is declared as int, and Me.lev contains the value 812

I don't understand why I get the following exception:

************** Exception Text **************
System.Runtime.InteropServices.COMException (0x80020008): Bad variable type.
at ADODB._Parameter.set_Value(Object pvar)
at ADODB.InternalParameter.set_Value(Object value)
at SugroBR.Leverandør.GetStoredProcedureCommandObject(Connection cn)
at SugroData.SugroDataObjekt.GetData(Boolean bClose)
 
M

Marina

My guess is, then it is an exception trying to convert a long to an integer.
Try casting it to an integer explicitly in the assignment.
 
G

Guest

Well, Marina, I wasn't even aware that this wasn't .net code, thanks for
reminding me, this should of cause be implemented using .net code, I will
look in to that. It is just a piece of code I found, that worked, and I put
it into my VB.NET app, and it worked. It has worked fine for some time, and
only stopped working because somebody (Me) tried starting the app from a
Terminal Server Client. Maybe it also has to do with versioning of ADO
components, but I hope the problem will disappear when it is converted to
..net code.

Anyway - I will try to fix it with a long to int cast first.

Peter
 
G

Guest

Yep. That did it. Thanks for your help.

Peter

Marina said:
My guess is, then it is an exception trying to convert a long to an integer.
Try casting it to an integer explicitly in the assignment.
 
G

Guest

Marina,

Actually, when I come to think about this a little closer, I am a bit
confused about data types here, maybe I am using the wrong data type
Client-side. My VB app uses a long to handle the int from the database,
because that was what I thought was the correct way to do it. But if I have
to convert it to integer to call an SP with a parameter of type int, this
tells me, that maybe I should use the integer data type client-side generally
to handle SQL server int fields?

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top