Solved

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

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

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.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

691 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