G
Guest
I am getting a NullReferenceException in System.Data.DataTable.ResetIndexes
when I do a DataSet.Merge. Most of the time this works just fine, but under
some situations, which of course I can’t determine, I get this exception.
Can you tell me what to look for?
System.NullReferenceException: Object reference not set to an instance of an
object.
at System.Data.DataTable.ResetIndexes()
at System.Data.Merger.MergeTable(DataTable src, DataTable dst)
at System.Data.Merger.MergeTableData(DataTable src)
at System.Data.Merger.MergeDataSet(DataSet source)
at System.Data.DataSet.Merge(DataSet dataSet, Boolean preserveChanges,
MissingSchemaAction missingSchemaAction)
at System.Data.DataSet.Merge(DataSet dataSet, Boolean preserveChanges)
at NAMISM.BusinessClasses.Global.UpdateDB(ArrayList dataAdapters,
ArrayList tableNames, DataSet dataSet) in
C:\NAMISM\BusinessClasses\Global.cs:line 150
Global.UpdateDB is this:
public static void UpdateDB(ArrayList dataAdapters, ArrayList tableNames,
DataSet dataSet)
{
int cnt;
if (dataAdapters.Count == tableNames.Count)
{
cnt = dataAdapters.Count;
}
else
{
throw new Exception ("Array sizes must match");
}
SqlTransaction sqlTrans = Global.SqlConn.BeginTransaction();
for (int i=0;i<cnt;i++)
{
((SqlDataAdapter)dataAdapters).InsertCommand.Connection = SqlConn;
((SqlDataAdapter)dataAdapters).DeleteCommand.Connection = SqlConn;
((SqlDataAdapter)dataAdapters).UpdateCommand.Connection = SqlConn;
((SqlDataAdapter)dataAdapters).InsertCommand.Transaction = sqlTrans;
((SqlDataAdapter)dataAdapters).DeleteCommand.Transaction = sqlTrans;
((SqlDataAdapter)dataAdapters).UpdateCommand.Transaction = sqlTrans;
}
try
{
// first do Inserts
DataSet dsInsert = dataSet.GetChanges(DataRowState.Added);
if (dsInsert != null)
{
for (int i=0;i<cnt;i++)
{
((SqlDataAdapter)dataAdapters).Update
(dsInsert.Tables[tableNames.ToString()]);
}
}
// now do Updates
DataSet dsUpdate = dataSet.GetChanges(DataRowState.Modified);
if (dsUpdate != null)
{
for (int i=0;i<cnt;i++)
{
((SqlDataAdapter)dataAdapters).Update
(dsUpdate.Tables[tableNames.ToString()]);
}
}
// finally do Deletes in reverse order
DataSet dsDelete = dataSet.GetChanges(DataRowState.Deleted);
if (dsDelete != null)
{
for (int i=cnt-1; i>=0; i--)
{
((SqlDataAdapter)dataAdapters).Update
(dsDelete.Tables[tableNames.ToString()]);
}
}
sqlTrans.Commit();
//merge rows re-retrieved from the DB into the original dataset
if (dsInsert != null) dataSet.Merge (dsInsert, false);
if (dsUpdate != null) dataSet.Merge (dsUpdate, false); //<<== line 150
if (dsDelete != null) dataSet.Merge (dsDelete, false);
// tell the original dataSet everything is applied
dataSet.AcceptChanges();
}
catch (SqlException e)
{
sqlTrans.Rollback();
throw new AodSqlException(e, "Database Update Failed");
}
}
when I do a DataSet.Merge. Most of the time this works just fine, but under
some situations, which of course I can’t determine, I get this exception.
Can you tell me what to look for?
System.NullReferenceException: Object reference not set to an instance of an
object.
at System.Data.DataTable.ResetIndexes()
at System.Data.Merger.MergeTable(DataTable src, DataTable dst)
at System.Data.Merger.MergeTableData(DataTable src)
at System.Data.Merger.MergeDataSet(DataSet source)
at System.Data.DataSet.Merge(DataSet dataSet, Boolean preserveChanges,
MissingSchemaAction missingSchemaAction)
at System.Data.DataSet.Merge(DataSet dataSet, Boolean preserveChanges)
at NAMISM.BusinessClasses.Global.UpdateDB(ArrayList dataAdapters,
ArrayList tableNames, DataSet dataSet) in
C:\NAMISM\BusinessClasses\Global.cs:line 150
Global.UpdateDB is this:
public static void UpdateDB(ArrayList dataAdapters, ArrayList tableNames,
DataSet dataSet)
{
int cnt;
if (dataAdapters.Count == tableNames.Count)
{
cnt = dataAdapters.Count;
}
else
{
throw new Exception ("Array sizes must match");
}
SqlTransaction sqlTrans = Global.SqlConn.BeginTransaction();
for (int i=0;i<cnt;i++)
{
((SqlDataAdapter)dataAdapters).InsertCommand.Connection = SqlConn;
((SqlDataAdapter)dataAdapters).DeleteCommand.Connection = SqlConn;
((SqlDataAdapter)dataAdapters).UpdateCommand.Connection = SqlConn;
((SqlDataAdapter)dataAdapters).InsertCommand.Transaction = sqlTrans;
((SqlDataAdapter)dataAdapters).DeleteCommand.Transaction = sqlTrans;
((SqlDataAdapter)dataAdapters).UpdateCommand.Transaction = sqlTrans;
}
try
{
// first do Inserts
DataSet dsInsert = dataSet.GetChanges(DataRowState.Added);
if (dsInsert != null)
{
for (int i=0;i<cnt;i++)
{
((SqlDataAdapter)dataAdapters).Update
(dsInsert.Tables[tableNames.ToString()]);
}
}
// now do Updates
DataSet dsUpdate = dataSet.GetChanges(DataRowState.Modified);
if (dsUpdate != null)
{
for (int i=0;i<cnt;i++)
{
((SqlDataAdapter)dataAdapters).Update
(dsUpdate.Tables[tableNames.ToString()]);
}
}
// finally do Deletes in reverse order
DataSet dsDelete = dataSet.GetChanges(DataRowState.Deleted);
if (dsDelete != null)
{
for (int i=cnt-1; i>=0; i--)
{
((SqlDataAdapter)dataAdapters).Update
(dsDelete.Tables[tableNames.ToString()]);
}
}
sqlTrans.Commit();
//merge rows re-retrieved from the DB into the original dataset
if (dsInsert != null) dataSet.Merge (dsInsert, false);
if (dsUpdate != null) dataSet.Merge (dsUpdate, false); //<<== line 150
if (dsDelete != null) dataSet.Merge (dsDelete, false);
// tell the original dataSet everything is applied
dataSet.AcceptChanges();
}
catch (SqlException e)
{
sqlTrans.Rollback();
throw new AodSqlException(e, "Database Update Failed");
}
}