Groups | Blog | Home
all groups > dotnet interop > february 2008 >

dotnet interop : Unable to cast COM object of type 'ADODB.RecordsetClass' to class type 'System.Object[]'



Scotty
2/20/2008 7:03:12 AM
Hi everyone,

I am using Visual Basic 2005. My project is in vb.net.
After executing a request on an Access Table, I would like to copy my
recordset into an array as such:

Dim ConnectionString As String =
"Provider=Microsoft.Jet.OLEDB.
4.0; Data Source=c:\myDataBase.mdb"
Dim sql As String = "SELECT * FROM [myTable];"
Dim conn1 As ADODB.Connection = New ADODB.Connection
conn1.Open(ConnectionString)
Dim rs As New ADODB.Recordset
rs = conn1.Execute(sql)
Dim tmp() as object = rs

The error message I get is:

Unable to cast COM object of type 'ADODB.RecordsetClass' to class type
'System.Object[]'. 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.

Norman Yuan
2/20/2008 12:01:56 PM
Because ADODB.Recordset IS NOT an array of object instance, it is a single
object instance.
So, you can

Dim obj As Object=rs

but not

Dim obj() As Object=rs

BTW, try not to use ADODB COM interop; using ADO.NET instead, unless there
is reason you really cannot.


[quoted text, click to view]
AddThis Social Bookmark Button