Home > Net >  System.Data.DuplicateNameException: A column named '..' already belongs to this DataTable
System.Data.DuplicateNameException: A column named '..' already belongs to this DataTable

Time:01-24

How do I fix Exception Details: System.Data.DuplicateNameException: A column named '..' already belongs to this DataTable. error when I used join inside the sql query? It works without join (obviously)

    public partial class _Default : Page
    {
        HanaConnection conn;
        protected void Page_Load(object sender, EventArgs e)
        {
        
        conn = new HanaConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Hana"].ConnectionString);
        conn.Open();
         
        using (HanaCommand cmd = new HanaCommand("SELECT top 3* FROM A T0 LEFT OUTER JOIN B T1 ON T1.\"something\"= T0.\"somethingsecond\" ", conn))
        {
            HanaDataReader productInfoReader = cmd.ExecuteReader();
            GridView1.DataSource = productInfoReader;
            GridView1.DataBind();
        };
        conn.Close();
        
       
 
    }
}

CodePudding user response:

Presumably you are joining two tables that both contain a column with a particular name but you're not joining on that column. This would produce a result set with two columns with identical names and that would cause that exception when a DataTable was generated based on that schema.

Instead of using a wildcard, you should specify the columns to include explicitly and then provide an alias for one or both of those columns. The DataTable will then use the alias and there will be no column name clash. As an example, instead of this:

SELECT *
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.Id = t2.Table1Id

use this:

SELECT t1.Name AS T1Name, t2.Name AS T2Name, ...
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.Id = t2.Table1Id
  • Related