'A column named [column name] already belongs to this DataTable
private void BindFields()
{
DataTable table = Globals.ConvertDataReaderToDataTable(DataProvider.GetFields());
_fieldCount = table.Rows.Count;
dataGrid.DataSource = table;
dataGrid.DataBind();
}
The ConvertDataReaderToDataTable, provided by the DotNetNuke platform, throws this exception :
A column named [column name] already belongs to this DataTable
I do have columns with the same names in different tables, but they are primary/foreign key pairs and thus have the same values. What would you do to solve this problem?
Solution 1:[1]
This happens when you have the same column selected in your SQL JOIN Statements. I fixed this problem by removing the the duplicate column from my sql join like this
Change This
SELECT O.Section,S.CurriculumTblCode,
S.Section ,S.SectionSettingTblCode FROM
otf.ViewOneToFiveGroups O
INNER JOIN
SectionSetting S ON O.SectionSettingTblCode= S.SectionSettingTblCode`
To this, Just by removing the duplicate S.Section
SELECT O.Section,S.CurriculumTblCode,S.SectionSettingTblCode
FROM
otf.ViewOneToFiveGroups O
INNER JOIN SectionSetting S ON O.SectionSettingTblCode=
S.SectionSettingTblCode
Solution 2:[2]
You would typically only use one of the columns in the data source. Short of that, you would alias one of the other columns.
Using only one of the duplicates
Select a.SomeColumn, b.OtherStuff
From TableA a
Inner Join TableB b
on a.SomeColumn = b.SomeColumn
Or using an alias
Select a.SomeColumn, b.SomeColumn as SomeColumnB, b.OtherStuff
From TableA a
Inner Join TableB b
on a.SomeColumn = b.SomeColumn
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Anthony Pegram |