Hi Bill,
After doing some research on your sample project, I find that it's the data
type of the DspPltSide field in the Dsp database table that causes the
problem. The data type of the DspPltSide field is varchar(1). I modify the
data type to char(1), the problem disappears.
As for how to capture the exception, let's see when the exception occurs.
Run the application and a DataGrid shows with the data retrieved from the
JobOrderTable. When I click a row in the DataGrid, the DataGrid tries to
show the row details. Since the row details contains a DataGrid which is
bound to the Dsps property of the JobOrderTable, LINQ to SQL retrieves the
child records from the Dsp database table corresponding to the specified
parent record in the JobOrderTable. Due to the improper data type of the
DspPltSide field in the Dsp table, the FormatException occurs.
The above behavior of LINQ to SQL is called deferred loading, which means
when you query for an object, you actually retrieve only the object you
requested. The related objects are not automatically fetched at the same
time. You cannot see the fact that the related objects are not already
loaded, because an attempt to access them produces a request that retrieves
them.
Since the data retrieving happens in the main UI thread, we can capture
this FormatException by handling the
Application.DispatcherUnhandledException event. Open the App.xaml file and
add the following attribute to subscribe the DispatcherUnhandledException:
<Application x:Class="ScAggScale.App"
DispatcherUnhandledException="Application_DispatcherUnhandledException"
....>
In the App.xaml.cs file, add the following event handler:
public partial class App:Application
{
private void Application_DispatcherUnhandledException(object sender,
System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
MesssageBox.Show("Unhandled Exception:" + e.Exception.Message);
e.Handled = true;
}
}
For more information on handling unhandled exception, please read the
following MSDN document:
http://msdn.microsoft.com/en-us/library/system.windows.application.dispatche
runhandledexception.aspx
Hope this helps.
If you have any question, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support