开发者

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?


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


I simply used

DataTable dataTable = new DataTable();
dataTable.Load(dataReader);

instead of DotNetNuke's

DataTable dataTable = Globals.ConvertDataReaderToDataTable(dataReader);

and I don't get the exception anymore.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜