Solved

C# error "specified cast is not valid"

Posted on 2004-03-30
3
604 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
ID: 10714924
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
ID: 10714942
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
ID: 10714968
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

773 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