Link to home
Start Free TrialLog in
Avatar of OddiC
OddiC

asked on

Invalid Cast Exception when trying to set DataGrid DataSource = IDataReader object (why??...)

Here is my code which calls a function that returns an IDataRreader object (I have verified that the IDataReader is populated with the correct data set I wish to use as my DataSource):

      dgMyGrid.DataSource = GetIDataReaderDataSource()

        Try
            dgBillTo.DataBind()   'fails on this line with "Invalid Cast Exception"
        Catch ex As Exception
        End Try

Can someone tell me why this is occuring and what I can do alternatively to make it work?

Thanks much in advance.
Avatar of imitchie
imitchie
Flag of New Zealand image

have you defined all your grid columns are non-nullable types, i.e. "int" instead of "int?"
if you have, then any nulls in the data will cause that error
ASKER CERTIFIED SOLUTION
Avatar of Sammy
Sammy
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of OddiC
OddiC

ASKER

I am working with 1.1

I am not sure how I define the grid column types as they are not being auto-generated and are defined at runtime based on the datasource.

Any thoughts?
Avatar of OddiC

ASKER

I figured it out. I needed to use DataTable instead of DataReader (my cast on ItemDataBound was not working with DataReader).

This is what was failing because the datasource was datareader:

 Dim odr As DataRowView = DirectCast(e.Item.DataItem, DataRowView)

Does anyone know how the above can be accomplished with a DataSource of DataReader?? (just for piece of mind).

Thanks.