SystemNullReferenceException using DataSet.Merge

C

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
 
C

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top