I'm having trouble with an existing piece of merge data table code I can't seem to get my head around
All the rest of the code I inherited, including what I have had to modify is working properly with this one exception. I have 2 data tables, first table is provided via .csv. The second table is pulled from other sources.
The issue I am having is that the code below is returning the following error:
There is no row at position (end position)
I understand that I should look to remedy the null values in the second table, but I need to return null values with the remainder of the code.
Is the above error generated with the for (int i-0) line? If so, how would I go about modifying this merge operation?
DataTable netb = MergeDataTables(dt, temp);
return netb;
static DataTable MergeDataTables(DataTable table1, DataTable table2)
{
DataTable table3 = table1.Copy();
foreach (DataColumn dc in table2.Columns)
{
table3.columns.Add(dc.ColumnName).DataType = dc.DataType;
}
for (int i = 0; < table3.Rows.Count; i++)
{
foreach (DataColumn dc in table2.Columns)
{
string col = dc.ColumnName;
table3.Rows[i][col] = table2.Rows[i][col];
}
}
Hey @BrianG probably your table3 is more large than table2, lets say that your table3 has 10 rows and table2 has 5, when you are in the seventh iteration your table2 will try to reach the position 6 that doesn't exists in table2, then it throws the error. You have to create a validation for that.