I'm having trouble getting a table with relationships. The problem arose that I had a complex query with many relationships, when I asked the question they helped me make the query efficient and fast, but when I placed it in my VB.NET-based program 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 the screen through a datagridview all the products, simple words but complex to carry out.
I have made 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 I run it in the database server console and it answers correctly without errors, and quickly, this query is the answer to another question, the link to the question is below.
The dataset doesn't have any table loaded so far, but after loading this table, I need to add more, so I can't use datatable, I have to use if or if a dataset.
As you can see, in the code, when filling in the dataset, "listadodeproducto" gives the error (Could not be enabled...), I have tried everything but I can't find a solution.
All query details are in this question: stackoverflow
From already thank you very much
If it happens to someone! I solved this problem in the following way
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!