Solved

C# error "specified cast is not valid"

Posted on 2004-03-30
3
603 Views
Last Modified: 2012-05-04
Hello Expert, I am getting an error when i try to do this
aDataSet = (DataSet) dataGrid1.DataSource;
Do you know why i would get specified cast is not valid i got it from
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformsdatagridclassdatasourcetopic.asp so i am not sure why it would not work. Can you help me?
Thanks
0
Comment
Question by:jvoros1
3 Comments
 
LVL 12

Accepted Solution

by:
dfiala13 earned 500 total points
Comment Utility
Make sure the DataSource on the grid is not null.  If you are trying to make this call after a postback, the DataSource property will be null and you will not be able to cast.

0
 
LVL 69

Expert Comment

by:Callandor
Comment Utility
The example shows
DataSet* myDataSet;
myDataSet = dynamic_cast<DataSet*> (dataGrid1->DataSource);

You want to cast a pointer to an object, not cast the actual object.
0
 

Author Comment

by:jvoros1
Comment Utility
Thanks for you help I see my problem now my dataGrid.DataSource = aDataTable not a aDataSet
so i changed it to aDataTable = (DataTable) dataGrid1.DataSource;
And it works great :)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

728 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

9 Experts available now in Live!

Get 1:1 Help Now