Solved

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

Posted on 2007-11-21
4
481 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

705 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

15 Experts available now in Live!

Get 1:1 Help Now