How to merge structure of two DataTables into one c#?

0

I have two data tables and want merge columns and create as third table. Do not want to lose ExtendedProperties so not Selecting DataColumn.ColumnName and adding as columns.

Code:

DataTable dt1 = new DataTable();
dt1.TableName = "dt1";
dt1.PrimaryKey = new DataColumn[] { dt1.Columns.Add("A") };
dt1.Columns.Add("B");
dt1.Columns.Add("C");
dt1.Columns.Add("D");

DataTable dt2 = new DataTable();
dt2.TableName = "dt2";
dt2.PrimaryKey = new DataColumn[] { dt2.Columns.Add("a") };
dt2.Columns.Add("b");
dt2.Columns.Add("c");
dt2.Columns.Add("d");

var dtResult = dt1.Clone();
dtResult.TableName = "dtResult";

Working fine till here

Now want to merge...

Multiple merge tries type 1

dtResult.Merge(dt2.Clone()); 
dtResult.Merge(dt2.Clone(), false);
dtResult.Merge(dt2.Clone(), true);
dtResult.Merge(dt2.Clone(), false, MissingSchemaAction.Add);
dtResult.Merge(dt2.Clone(), false, MissingSchemaAction.AddWithKey); 
dtResult.Merge(dt2.Clone(), true, MissingSchemaAction.Add);
dtResult.Merge(dt2.Clone(), true, MissingSchemaAction.AddWithKey);

In all above tries, Columns are merged into dtResult but throws NullReferenceException. StackTrace:

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=System.Data
  StackTrace:
   at System.Data.Merger.MergeSchema(DataTable table)
   at System.Data.Merger.MergeTableData(DataTable src)
   at System.Data.Merger.MergeTable(DataTable src)
   at System.Data.DataTable.Merge(DataTable table, Boolean preserveChanges, MissingSchemaAction missingSchemaAction)
   at Demo.Program.Test5() in C:\App\Demo\Program.cs:line 54
   at Demo.Program.Main(String[] args) in C:\App\Program.cs:line 36

Multiple merge tries type 2

dtResult.Merge(dt2.Clone(), false, MissingSchemaAction.Error);
dtResult.Merge(dt2.Clone(), true, MissingSchemaAction.Error);

In all above tries, Columns are NOT merged into dtResult and System.Data.DataException StackTrace:

System.Data.DataException
  HResult=0x80131920
  Message=Target table  missing definition for column a.
  Source=System.Data
  StackTrace:
   at System.Data.Merger.MergeSchema(DataTable table)
   at System.Data.Merger.MergeTableData(DataTable src)
   at System.Data.Merger.MergeTable(DataTable src)
   at System.Data.DataTable.Merge(DataTable table, Boolean preserveChanges, MissingSchemaAction missingSchemaAction)
   at Demo.Program.Test5() in C:\App\Demo\Program.cs:line 54
   at Demo.Program.Main(String[] args) in C:\App\Demo\Program.cs:line 36

Multiple merge tries type 3

dtResult.Merge(dt2.Clone(), false, MissingSchemaAction.Ignore);
dtResult.Merge(dt2.Clone(), true, MissingSchemaAction.Ignore);

In all above tries, Columns are NOT merged into dtResult and NO exception

c#
.net
datatable
nullreferenceexception
asked on Stack Overflow Jul 25, 2019 by Prem • edited Jul 25, 2019 by Prem

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0