Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when adding parameters to ADODB command in a .NET assembly

I have a .NET assembly that is being consumed by a classic ASP page. I've created a method that returns a ADODB recordset. In my ADODB command object I'm supplying parameters using the following format to a adCmdStoredProc CommandType property...

With ADODBCmd 
.ActiveConnection = ADODBConn
.Prepared = True
.CommandType = CommandTypeEnum.adCmdStoredProc
.NamedParameters = True
.CommandText = Sql_GetMyBook   
.Parameters.Append(.CreateParameter("@book", DataTypeEnum.adChar, ParameterDirectionEnum.adParamInput, 50, MyBook))
End With

I get a casting error ...

System.Exception was unhandled
Message=System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to class type 'ADODB.InternalParameter'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.

at line:

.Parameters.Append(.CreateParameter("@book", DataTypeEnum.adChar, ParameterDirectionEnum.adParamInput, 50, MyBook))

Any ideas?

Stored Proc:

ALTER PROCEDURE [dbo].[GetMybook]
    -- Add the parameters for the stored procedure here
    @book char(50)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT BookTitle, Author, PulishedDate
    FROM        Library
    WHERE       BookTitle=@book
like image 384
ihillVT Avatar asked Jun 02 '26 18:06

ihillVT


1 Answers

There is a difference (unintended or not) between the return value of the ".CreateParameter" method in Microsoft ActiveX Data objects Library

2.7 - Returns "ADODB.InternalParameter" (which is expected by the ADODB.Command object)

2.8 - Returns "System.__ComObject" (which the ADODB.Command can't handle or doesn't know what to do with)

For my purposes I had to change my reference from 2.8 to 2.7 library in order to append parameters created to the command object.

Thanks to Chris Behrens for helping me narrow down my search for a solution.

like image 99
ihillVT Avatar answered Jun 04 '26 10:06

ihillVT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!