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

Prem
  • 316
  • 1
  • 5
  • 23
  • Well, I am looking for the use of ```DataTable.Merge(DataTable table, Boolean preserveChanges, MissingSchemaAction missingSchemaAction)``` – Prem Jul 25 '19 at 06:01
  • You really need use debugger to find out which line throws an exception! Might be: `dtResult` is must be null? – Prashant Pimpale Jul 25 '19 at 06:02
  • I think try catch block will not solve your problem, you need to have a look at the reason of *null* – Prashant Pimpale Jul 25 '19 at 06:20
  • I added try-catch ``` var dtResult = dt1.Clone(); try { dtResult.Merge(dt2.Clone()); }catch(NullReferenceException ex) { } ``` After try catch, dtResult have all the required column. – Prem Jul 25 '19 at 06:21
  • dt1, dt2, dtResult, non of these are null. Also in StackTrace ```Merger.MergeSchema``` has problem which means ```dtResult``` is NOT null. In my code there's already null check for ```dt2```. – Prem Jul 25 '19 at 08:17
  • @brian-rasmussen – Prem Jul 25 '19 at 09:31
  • Unfortunelty the question is closed! But you can post an another question for as: `How to merge two datatables` which proper code – Prashant Pimpale Jul 25 '19 at 10:02

0 Answers0