Home All Groups Group Topic Archive Search About

SystemNullReferenceException using DataSet.Merge

Author
27 Jun 2006 9:12 AM
Classgenerator
Hi all,
i am using two typed datasets (based on the same Class) and get a
SystemNullReferenceException (Source: System.Data.dll / ResetIndexes())
when i try to merge like the following:

DS1.Merge( DS2.MyTable );

Then after handling the Exception DS2.MyTable is merged correctly into
DS1. All Data is correct!

EnforceConstraints = true;
Event MergedFailed is not raised.

Any Hints or ideas?

please let me know
Ronny

Author
28 Jun 2006 7:37 AM
Classgenerator
Hi All,

what i found out is that there is a problem in Merge! As a Workaround
which works fine, you have to remove the references from the indexes of
the corresponding tables and then call the private member
ResetIndexes() from DataTable. Then Merge. If your DataSet is bound to
a control, don't forget to refresh the datasource.

RemoveIndexReferences( DS1.Table[0] );
RemoveIndexReferences( DS2.Table[0] );

DS1.Merge( DS2.Table[0] );

private  void RemoveIndexReferences(DataTable tbl)
    {
      try
      {
        ArrayList indexes =
(ArrayList)typeof(DataTable).GetField("indexes",
          System.Reflection.BindingFlags.GetField |
System.Reflection.BindingFlags.Instance |
          System.Reflection.BindingFlags.NonPublic).GetValue(tbl);
        ArrayList indexList = new ArrayList();
        foreach (object index in indexes)
        {
          indexList.Add(index);
        }
        foreach (object index in indexList)
        {
          index.GetType().InvokeMember("RemoveRef",
System.Reflection.BindingFlags.Instance |
            System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.InvokeMethod,
            null, index, null);
        }
        ResetIndexes(tbl);
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.ToString());
      }
    }
    private void ResetIndexes(DataTable tbl)
    {
      try
      {

        typeof(DataTable).InvokeMember("ResetIndexes",
System.Reflection.BindingFlags.Instance |
          System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.InvokeMethod,
          null, tbl, null);
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.ToString());
      }
    }

It helped for me, hope, for you, too...
All the Best
Ronny

AddThis Social Bookmark Button