Solved

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

Posted on 2007-11-21
4
485 Views
Last Modified: 2011-09-20
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.
0
Comment
Question by:OddiC
  • 2
4 Comments
 
LVL 25

Expert Comment

by:imitchie
ID: 20332222
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
0
 
LVL 27

Accepted Solution

by:
Sammy earned 500 total points
ID: 20332952
I would check if the method returns a datareader first before you set it as the datasource
Dim DataReader as DataReader=GetIDataReaderDataSource()
If (Not (DataReader) Is Nothing) Then
'Bind here
End If
Try this as an alternative "does NOT in 1.1, only 2.0 and after"

Dim oDataTable As DataTable = New DataTable
oDataTable.Load(GetIDataReaderDataSource())
dgMyGrid.DataSource = oDataTable
0
 

Author Comment

by:OddiC
ID: 20350220
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?
0
 

Author Comment

by:OddiC
ID: 20350885
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.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now