Hi,
I have a DataGrid on a WinForm into which I am loading a DataSet with 1 record. I have try/catch blocks around all of my code. When I click in any column of the new row, I get an InvalidCastException reported as an unhandled exception, with the additional information "Specified cast is not valid". How can I trap this exception How can I find out where the invalid cast is Any ideas what is causing it
Thanks,
Royce

InvalidCastException in DataGrid
NewbeeVFP
You are doing something somewhere.
This is when you should have proper error handling.
For example you don't always want to show a user a stacktrace (which would give you the line number that has the problem) so you would have two error messages, a user frendly one and one for a developer, you would have a way to set this, like a setting in the app.config or something, to turn this on or off.
That way you can do
msgbox(ex.tostring)
which will show you the message and the stacktrace all in one message.
Hope this helps.
Arun Manglick
Thanks for your help. I think I have most of my problems figured out. However, I still have this strange exception trapping/handling problem where code with try/catch blocks around it is causing an exception that is not caught by my handler, as follows:
A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll
Additional information: System error.
This is my code:
private void btnUpdate_Click(object sender, System.EventArgs e)
{
try
{
//persist all changes from the data model to the database
daRoomRate.Update(dsRoomRate, "RoomRate");
}
catch(SqlException SqlEx)
{
//handle SQL Server specific errors
foreach(SqlError err in SqlEx.Errors)
{
MessageBox.Show("SQL Error " + err.Number + ": " + err.Message);
}
}
catch(Exception Ex)
{
//handle general errors
MessageBox.Show("Non-SQL Exception " + Ex.Message);
}
}
After entering data into all columns of my new row, I click the Update button. The debugger's exception dialog appears. When I click the Break button, the debugger points to the Update() call - my exception handler is never invoked. One other item of information: I am running the academic version of VS .NET 2003 and SQL Server 2000 Developer Edition.
Thanks,
Royce