Error al insertar datos con tablas relacionadas

Estoy haciendo un sistema de ventas y tengo un problema al insertar datos en mis tablas y no encuentro solución. Estas son las relaciones que tengo en mi DB y al insertar datos en mi tabla detalle_venta me aparece el error "The INSERT statement conflicted with the FOREIGN KEY constraint "FK_detalle_venta_producto". The conflicted occurred in database "despensa", table "dbo. Producto", column "codproducto". The statement has been terminated". ¿Alguien sabe por qué puede ser? Quiero insertar mediante un botón que se llama Guardar y el código es ese.

Private Sub btnguardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnguardar.Click

Try

Dim dts As New vdetalle_venta
Dim func As New fdetalle_venta

For Each row As DataGridViewRow In DataGridView1.Rows

dts.gcodproducto_dv = Convert.ToInt64(row.Cells("codproducto").Value())
dts.gcantidad = Convert.ToDouble(row.Cells("cantidad").Value())
dts.gprecio_unitario = Convert.ToDouble(row.Cells("precio").Value())
dts.gimporte = Convert.ToDouble(row.Cells("importe").Value())

Next

If func.insertar(dts) Then
If func.disminuir_stock(dts) Then

End If

MessageBox.Show("Artículo añadido a la venta", "vamos a añadir productos", MessageBoxButtons.OK, MessageBoxIcon.Information)

Else
MessageBox.Show("Artículo no fue añadido", "guardando registros", MessageBoxButtons.OK, MessageBoxIcon.Information)

End If

Catch ex As Exception

End Try

End Sub

1 Respuesta

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas