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