Error Casting ODBC objects to IDb Interfaces

  • Thread starter Thread starter Brad
  • Start date Start date
B

Brad

I have a generic data access component that uses
interfaces to allow for multiple data providers. (I.E.
ODBC, OleDb, Sql) I built the component in VB .NET 2002
and it worked fine. I have now upgraded the component to
VB .NET 2003 and get run time errors when trying to cast a
System.Data.Odbc.OdbcConnection object to the
IDbConnection interface. The error I receive is:
System.InvalidCastException: Specified cast is not valid.

Here is a subset of my code:

Dim objConnection As Object
Dim cnn As IDbConnection

objConnection = CreateDataObject(strConnectionClass)
' CreateDataObject returns a
System.Data.Odbc.OdbcConnection object
cnn = CType(objConnection, IDbConnection)


The error occurs when executing the last line.
The CreateDataObject function uses reflection to create
the ODBC connection object.

Was something in the ODBC class set changed between 2002
and 2003 that would impact the type of casting I am
attempting?

Thanks!
- Brad
 
Why don't you declare objConnection as IDbConnection, and have
CreateDataObject() return an IDbConnection? Maybe you'll find it is
something inside CreateDataObject causing the error?

Always define a type as strickly as possible.

Michael Lang, MCSD
 
Michael,

Thanks for the response. However I can't use your
suggestion because the CreateDataObject() function also
creates Command, DataAdapter, and DataParameter objects.
Which I then cast to their IDb interface equivalents. All
of these functions are generating the same
InvalidCastException.

- Brad
 
Back
Top