Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 data tables with same column names (Datatable data is from csv files). I use 'Datatable.Merge' option, but since one of the table's particular column's Datatype is not matched with the other table's that column, it is throwing error.

How do I merge 2 Data table irrespective of column datatype in C# code

What I have tried:

1. datatable1.Merge(datatable2);
2. datatable1.Merge(datatable2,false,MissingSchemaAction.Add);

Also tried:-

foreach (var row in dattable2.Rows)
{
datatable1.Rows.Add(row);
}
Posted
Updated 22-Mar-20 20:52pm
v2
Comments
ZurdoDev 20-Mar-20 8:19am    
Write code to loop through and set the particular columns' values.
ZurdoDev 20-Mar-20 8:19am    
Or, do it in the database directly. That's a better option if you can.
Maciej Los 20-Mar-20 9:13am    
Could you share your datatables structure and sample data?
Why do you need to merge data?
vivektp 23-Mar-20 3:23am    
Thanks for your reply..
My Data tables are filled by reading the CSV files. Both the Data tables column names are same but some of the column's data types are different. Due to this while merging I am getting error.
Richard MacCutchan 23-Mar-20 5:15am    
You cannot merge different types of data, they must all be the same. You will need to check each item as you process it and convert one to the other type.

1 solution

Do it in the database, and use a join to bring in data from both tables.

Caveat: The tables being joined must to have a column that can be compared for equality so you can join the appropriate rows from both tables. I would create a view that does it, and then you can just query the view for your data.
 
Share this answer
 
Comments
vivektp 23-Mar-20 3:21am    
My Data tables are filled by reading the CSV files. Both the Data tables column names are same but some of the column's data types are different. Due to this while merging I am getting error.
#realJSOP 23-Mar-20 5:31am    
You have to do a join with Linq. Google "Linq join".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900