Unhandled exception using EF
I've got the following method to delete a row from a table using a data grid.
Code:
private void FieldGrid_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
try
{
// get ids from row
var xref = (FieldMap)e.Row.DataBoundItem;
// delete data from database
var record = (from sf in this.Entities.Source_Field
where sf.SourceTableFieldMapID.Equals(xref.SourceMapId)
&& sf.DestinationTableFieldMapID.Equals(xref.DestinationMapId)
select sf).FirstOrDefault();
this.Entities.Source_Field.DeleteObject(record);
this.Entities.SaveChanges();
// rebind
this.BindFieldMappings();
}
catch (Exception ex)
{
this.HandleError(ex);
}
}
When I step through, everything runs fine and the row is deleted. Somewhere ( I've set the debugger to break on thrown and user handled exceptions) I get an error and the debugger breaks on Application.Run in the startup class. I think this is happening because entity framework is using a different thread. What I would like is for the exception to be caught in the event handler. This is the error
Code:
"System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index\r\n at System.Collections.ArrayList.get_Item(Int32 index)\r\n at System.Windows.Forms.DataGridViewRowCollection.SharedRow(Int32 rowIndex)\r\n at System.Windows.Forms.DataGridViewRowCollection.get_Item(Int32 index)\r\n at System.Windows.Forms.DataGridView.ProcessDeleteKey(Keys keyData)\r\n at System.Windows.Forms.DataGridView.ProcessDataGridViewKey(KeyEventArgs e)\r\n at System.Windows.Forms.DataGridView.OnKeyDown(KeyEventArgs e)\r\n at System.Windows.Forms.Control.ProcessKeyEventArgs(Message& m)\r\n at System.Windows.Forms.DataGridView.ProcessKeyEventArgs(Message& m)\r\n at System.Windows.Forms.Control.ProcessKeyMessage(Message& m)\r\n at System.Windows.Forms.Control.WmKeyChar(Message& m)\r\n at System.Windows.Forms.Control.WndProc(Message& m)\r\n at System.Windows.Forms.DataGridView.WndProc(Message& m)\r\n at System.Windows.Forms.Control
.ControlNativeWindow.OnMessage(Message& m)\r\n at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)\r\n at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)\r\n at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)\r\n at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)\r\n at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)\r\n at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)\r\n at System.Windows.Forms.Application.Run(Form mainForm)\r\n at SFHP_AutoTest.Program.Main() in C:\\TFS\\DEV\\SFHP\\Applications\\Auto QA Win Form\\Program.cs:line 28"