?
Solved

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

Posted on 2007-11-21
4
Medium Priority
?
492 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 Ageil earned 1500 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

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.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

801 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