0

I have 2 datagridviews in my winform project. one of them is a list with the names of my "combos", and the other one is supposed to show me all the information of the selected name with the following query which is the datasource to the datagrid2

SELECT        Combo.id_combo, Combo.name, Combo.price, Producto.name AS Expr1, Producto.price AS Expr2
FROM            Combo INNER JOIN
                         Detalle ON Combo.id_combo = Detalle.id_combo INNER JOIN
                         Producto ON Detalle.id_producto = Producto.id_producto
WHERE        (Combo.name = @name)

but when I execute the code the following error pops up: Failed to enable constraints. One or more rows contain values ​​violating the NON-NULL, UNIQUE, or FOREIGN-KEY constraints.

I know the query works since I've tested it on my SQL server but it seems I can't set it as the datasource for my table.

I'm setting the table datasource like this, once the click event on the first datagrid is detected and the name is captured

datagridview2.datasource=combo.detail(name);

combo.detail is a function of the class I use to connect to my database which will execute the query with the name as the parameter to send.

Why is it not working?

ucMedia
  • 4,105
  • 4
  • 38
  • 46
user2793090
  • 183
  • 2
  • 2
  • 10

1 Answers1

0

Your problem is that your join is not valid due the column values, they are either null or duplicated values which are not valid as you are trying to join them. Look up your table records in Combo ,Detalle , Producto and check to see if you have duplicated or null values and try to fix them before you run your query. I strongly suggest you to make foreign constraints between the fields. Here is a link which may help you further :Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints

Community
  • 1
  • 1
Behrad Farsi
  • 1,110
  • 13
  • 25
  • But if this is true shouldnt the query not work in the sql query builder as well? the problem is i do get a table of the expected results with this query, only when i want to asign this table to a datagridview is where this error pops, thx for the link. – user2793090 Jan 30 '14 at 16:54