DataView NullReferenceException in OnListChanged

I have a rather complicated WinForms application that uses data binding to bind strongly typed DataTables to controls (not sure if this fact matters here). An exception occurs when changing a column causes logic that updates another column of the same row (or at least that's my theory).

Example: a customer record is bound to a ComboBox with a contact list in Customer :: MarketingContactId. When a contact changes, there are several text fields and flags that will be updated to allow editing of these contact details. One of these flags has an event handler in CheckChanged.

void MarketingContactIsFulltimeChangedHandler(object sender, EventArgs e)
{
   var contact = m_row.MarketingContact;
   if (contact != null && contact.IsFullTimeEmployee)
   {
      var i = m_row.Institution;
      if (i.CreditLevel > m_row.CreditLevel)
         m_row.CreditLevel = i.CreditLevel;
   }
}

m_row will be updated by the Client. However, there is no error in this code, but in fact it has been raised from the setting of the MarketingContactId field. Here is the call stack that confirms this:

System.Data.dll! System.Data.DataView.OnListChanged(System.ComponentModel.ListChangedEventArgs e = {System.ComponentModel.ListChangedEventArgs}) 1433 + 0x2c #     System.Data.dll! System.Data.DataView.IndexListChanged( , System.ComponentModel.ListChangedEventArgs e = {System.ComponentModel.ListChangedEventArgs}) 1299 # System.Data.dll! System.Data.DataView.IndexListChangedInternal(System.ComponentModel.ListChangedEventArgs e) 1324 #
System.Data.dll! System.Data.DataViewListener.IndexListChanged(System.ComponentModel.ListChangedEventArgs e) 76 + 0x7 #
System.Data.dll! System.Data.Index.OnListChanged.AnonymousMethod(System.Data.DataViewListener , System.ComponentModel.ListChangedEventArgs args, bool arg2, bool arg3) 803 + 0x7 #
System.Data.dll! System.Data.Listeners.Notify(System.ComponentModel.ListChangedEventArgs arg1 = {System.ComponentModel.ListChangedEventArgs}, bool arg2 = false, bool arg3 = false, System.Data.Listeners.Action action = {Method = {Void b__2 (System.Data.DataViewListener, System.ComponentModel.ListChangedEventArgs, Boolean, Boolean)}}) 1029 + 0x1a # System.Data.dll! System.Data.Index.OnListChanged(System.ComponentModel.ListChangedEventArgs e) 805 # System.Data.dll! System.Data.Index.OnListChanged(System.ComponentModel.ListChangedType changedType, int newIndex, int oldIndex) 788 # System.Data.dll! System.Data.Index.RecordStateChanged(int oldRecord, System.Data.DataViewRowState oldOldState, System.Data.DataViewRowState oldNewState, int newRecord, System.Data.DataViewRowState newOldState, System.Data.DataViewRowState newNewState) 903 + 0x10 #
System.Data.dll! System.Data.DataTable.RecordStateChanged(int record1 = 0, System.Data.DataViewRowState oldState1 = , System.Data.DataViewRowState newState1 = , int record2 = 1, System.Data.DataViewRowState oldState2 = , System.Data.DataViewRowState newState2 = ) 3473 + 0x18 # System.Data.dll! System.Data.DataTable.SetNewRecordWorker(System.Data.DataRow row = "CustomerRow: Id: -1", int suggestRecord, System.Data.DataRowAction = , bool isInMerge, int position, bool fireEvent = true, out System.Exception deferredException = null) 3935 + 0x16 #
System.Data.dll! System.Data.DataTable.SetNewRecord(System.Data.DataRow , int Record, System.Data.DataRowAction, bool isInMerge, bool fireEvent) 3824 # System.Data.dll! System.Data.DataRow.SetNewRecord(int record) 1160 # System.Data.dll! System.Data.DataRow.EndEdit() 631 + 0xa #
System.Data.dll! System.Data.DataRow.this [System.Data.DataColumn].set(System.Data.DataColumn , ) 343 #

, , System.Data.dll MS Symbol Server Visual Studio. OnListChanged:

// snippet from System.Data.DataView::OnListChanged(ListChangedEventArgs e)
if (onListChanged != null) { 
   if ((col != null) && (e.NewIndex == e.OldIndex)) {
      ListChangedEventArgs newEventArg = new ListChangedEventArgs(e.ListChangedType, e.NewIndex, new DataColumnPropertyDescriptor(col)); 
    /*Here is where VS breaks for the exception*/ onListChanged(this, newEventArg);
    } else {
         onListChanged(this, e); 
    }
} 

, - , onListChanged!= null . , DataRow EndEdit . , . - SO , , ?

kludge BeginInvoke MarketingContactIsFulltimeChangedHandler , CreditLevel.

+3

Source: https://habr.com/ru/post/1722796/


All Articles