Search code examples
c#datatableconstraintstableadapter

Failed to enable constraints using c# tableadapter


I have probably a really silly question but after lots of research on google and trying several things I didn't find any solution for my issue.

I am using a TableAdapter and run the following query:

 SELECT        
    table1.column2, table2.column2
 FROM          
    table2 
 INNER JOIN
    table1 ON table2.ID = table1.table2ID
 WHERE        
    (table1.column2 = @something)

Running the following code

   DataTable DT = new DataTable();
   DT = TableAdapter.GetByColumn2("something");

works fine unless the resulting DataTable is empty. When it returns a value I get the following error:

ConstraintException was unhandled Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.

If I run the query manually it returns exactly the information I need at this point and SQL doesn't complain about any constraints.

I am a real newbie to C# and would really appreciate if anybody could help.

Thanks...


Solution

  • Right now, you query return only 1 column, if you want both column in your result set use alias

    http://sqlfiddle.com/#!3/d6d68/4

    You have to use alias in your sql query so that query return both column.

    SELECT table1.column2 as Table1Column2, table2.column2 as Table2Column2
        FROM  table2 INNER JOIN
            table1 ON table2.ID = table1.table2ID
        WHERE  (table1.column2= @something)
    

    This may be not the exact problem OP faced, but OP has to include the Error he gets to get exact solution.