Unable to cast COM object of type 'ADODB.RecordsetClass' to classtype 'System.Object[]'

S

Scotty

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.

Does anyone know why I am not allowed to perform this operation ?
 
N

Norman Yuan

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.
 
M

Michel Posseth [MCP]

Scotty said:
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.

Does anyone know why I am not allowed to perform this operation ?

Hello Scotty

just change
Dim tmp() as object = rs

to

Dim tmp() as object = rs.Getrows

and you have what you want

this link is valid as reference how to retrieve the records and fields
although it states Access developer it is exactly the same for classic ADO

http://msdn2.microsoft.com/en-us/library/bb221041.aspx


HTH

Michel Posseth [MCP]
 
N

Niko Suni

Hey Michel,

"Dim tmp() as object" declares an array of objects. Since the recordset is a
single object, it cannot be cast to an array by itself.

To correct this, rewrite the statement without the brackets "()".

HTH,
-Niko
MVP, Windows/DirectX


Michel Posseth said:
Scotty said:
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.

Does anyone know why I am not allowed to perform this operation ?

Hello Scotty

just change
Dim tmp() as object = rs

to

Dim tmp() as object = rs.Getrows

and you have what you want

this link is valid as reference how to retrieve the records and fields
although it states Access developer it is exactly the same for classic ADO

http://msdn2.microsoft.com/en-us/library/bb221041.aspx


HTH

Michel Posseth [MCP]
 
M

Michel Posseth [MCP]

Hello Niko ,
"Dim tmp() as object" declares an array of objects. Since the recordset is a
single object, it cannot be cast to an array by itself.

no not the recordset itself but the rs.Getrows method as i showed can

Dim tmp() as object = rs.Getrows

the rs.Getrows method returns the recordset as a variant array in VB.Net ,
C# you can use a object array instead

This wil return a multi dimensioned object array containing all row and
column data

The link i provided ( VBA ) shows how to loop through the records and
fields

HTH

Michel




Niko Suni said:
Hey Michel,

"Dim tmp() as object" declares an array of objects. Since the recordset is a
single object, it cannot be cast to an array by itself.

To correct this, rewrite the statement without the brackets "()".

HTH,
-Niko
MVP, Windows/DirectX


Michel Posseth said:
Scotty said:
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.

Does anyone know why I am not allowed to perform this operation ?

Hello Scotty

just change
Dim tmp() as object = rs

to

Dim tmp() as object = rs.Getrows

and you have what you want

this link is valid as reference how to retrieve the records and fields
although it states Access developer it is exactly the same for classic ADO

http://msdn2.microsoft.com/en-us/library/bb221041.aspx


HTH

Michel Posseth [MCP]
 

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