Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2007-11-21
4
Medium Priority
?
496 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

618 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