Wednesday, October 3, 2012

Column 'column_name' already belongs to another DataTable

In a scenario where I added columns using AddRange(), I encountered an error as specified by the title.
Here's the C# code:
Code:
table1.Columns.AddRange(new DataColumn[] { col1, col2, col3 });  
table2.Columns.AddRange(new DataColumn[] { col1, col2, col3 });
Where col1, col2, col3 are DataColumn objects.
What I did was to initialize columns in the addrange method. But there are other solutions in the forums.
Code:
table1.Columns.AddRange(new DataColumn[] { new DataColumn("ID"), new DataColumn("WEB ID"), new DataColumn("URL") });  
table2.Columns.AddRange(new DataColumn[] { new DataColumn("ID"), new DataColumn("WEB ID"), new DataColumn("URL") });

0 comments:

Post a Comment