Problem with relations in DATASET obtained from database

2

I'm having trouble getting a table with relationships. The problem arose that I had a complex query with many relationships, when asking the question they helped me to make the query that is efficient and fast, but when placing it in my program based on VB.NET I got the following error:

No se pudieron habilitar las restricciones. Una o varias filas contienen valores que infringen las restricciones NON-NULL, UNIQUE o FOREIGN-KEY.

The system must show on screen through a datagridview all products, simple words but complex to make.

I have classes for each thing, because the system is becoming more complex and I have separated everything into classes.

The datagridview uses a bindingsource, as follows:

Public datosdeproductos As New BindingSource
Private listadodeproducto As New DataSet
datosdeproductos.DataSource = listadodeproducto
Using conexion As New MySqlConnection(conexionstring)
    conexion.Open()
    If listadodeproducto Is Nothing Then
        listadodeproducto = New DataSet()
        listadodeproducto.BeginInit()
    End If
    dim consulta as string = "SELECT pr.id, pr.nombre as Nombre, cp.nombre AS Categoria, pv.nombre AS Proveedor, cantidad as Cantidad, stockminimo AS 'Stock Mínimo', (p.precio) AS Precio, (p.precio2) AS 'Precio %', (p.costo) AS 'P. costo', (p.ganancia) AS Ganancia, ultimamodificacion AS 'Fecha última modificación', cod.codigobarras AS 'Código de Barras' FROM productos pr INNER JOIN precios p ON p.idproducto = pr.id INNER JOIN proveedores pv ON pv.id = pr.proveedor INNER JOIN categoriadeproductos cp ON cp.id = pr.categoria INNER JOIN codigobarras cod ON cod.idproducto = pr.id WHERE p.numerodepreciodelista = 0 AND pr.eliminado = 0;"
    Using adaptador As New MySqlDataAdapter(consulta, conexion)
        adaptador.Fill(listadodeproducto, "listadodeproductos")
    End using
    conexion.close
End using
planillalistadeproductos.datasource = listadodeproducto ' 
planillalistadeproductos es el datagridview
planillalistadeproductos.DataMember = "listadodeproductos"

The query I am using is the following:

SELECT 
pr.id, 
pr.nombre as Nombre,
cp.nombre AS Categoria,
pv.nombre AS Proveedor, 
cantidad as Cantidad,
stockminimo AS 'Stock Mínimo',
(p.precio) AS Precio,
(p.precio2) AS 'Precio %',
(p.costo) AS 'P. costo',
(p.ganancia) AS Ganancia,
ultimamodificacion AS 'Fecha última modificación',
cod.codigobarras AS 'Código de Barras'
FROM productos pr 
INNER JOIN precios p ON p.idproducto = pr.id
INNER JOIN proveedores pv ON pv.id = pr.proveedor 
INNER JOIN categoriadeproductos cp ON cp.id = pr.categoria
INNER JOIN codigobarras cod ON cod.idproducto = pr.id -- Probablemente devuelva mas de un resultados si tenes mas de un Codigo de Barra para un solo producto
WHERE 
p.numerodepreciodelista = 0 AND pr.eliminado = 0;

This query is functional, I mean that I execute it in the server console of the database and I answer correctly without errors, and quickly, this query is the answer of another question, the link of the question is found below.

The dataset, does not have any tables loaded so far, but after loading this table, I need to add more, therefore, I can not use datatable, I have to use if or if a dataset.

As you can see, in the code, when filling in the dataset, "productlist" gives the error (Could not be enabled ...), I tried everything but I can not find a solution.

All the details of the query are in this question: stackoverflow

Thank you very much

    
asked by Bernardo Harreguy 19.08.2017 в 18:51
source

1 answer

1

If someone happens to you! solve this problem in the following way

SELECT productos.id,
       productos.nombre AS Nombre,
       categoriadeproductos.nombre AS Categoria,
       proveedores.nombre AS Proveedor,
       cantidad AS Cantidad,
       stockminimo AS 'Stock Mínimo',
       (precios.precio) AS Precio,
       (precios.precio2) AS 'Precio %',
       (precios.costo) AS 'P. costo',
       (precios.ganancia) AS Ganancia,
       ultimamodificacion AS 'Fecha última modificación',
       codigobarras.codigobarras AS 'Código de Barras'
FROM productos
INNER JOIN precios
    ON precios.numerodepreciodelista = 0 AND precios.idproducto = productos.id 
INNER JOIN codigobarras
    ON codigobarras.idproducto = productos.id
INNER JOIN (proveedores, categoriadeproductos)
ON proveedores.id = productos.proveedor
    AND categoriadeproductos.id = productos.categoria
WHERE productos.eliminado = 0
GROUP BY productos.id

In simple words I have removed the tables to perform the query virtually, and most importantly I have added KEY indexes in each identifier of the related tables that are referenced, and the result is simple, and the query is more effective ! thanks to those who tried to help me!

    
answered by 19.08.2017 / 21:45
source