Error Casting ODBC objects to IDb Interfaces

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
 
M

Michael Lang

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
 
B

Brad

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
 

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