Buen día.
Sucede que estoy haciendo mi proyecto final de Visual Basic .Net, y tengo un pequeño problema al añadir mas de 1 objeto a la base de datos mediante un DataGridView.
Dim Cmd As New OleDbCommand("insert into Ventas (Producto,Cliente,Fecha,Usuario,Precio,ConDeb,Cantidad,Codigo) Values (@Producto,@Cliente,@Fecha,@Usuario,@Precio,@ConDeb,@Cantidad,@Codigo)", ConBD)
For Each Row As DataGridViewRow In DGV.Rows
Cmd.Parameters.Clear()
Cmd.Parameters.AddWithValue("@Producto", DGV.CurrentRow.Cells(1).Value)
Cmd.Parameters.AddWithValue("@Cliente", CBC.Text)
Cmd.Parameters.AddWithValue("@Fecha", DateTime.Now.ToShortDateString)
Cmd.Parameters.AddWithValue("@Usuario", NombreUser)
Cmd.Parameters.AddWithValue("@Precio", CInt(DGV.CurrentRow.Cells(3).Value))
Cmd.Parameters.AddWithValue("@ConDeb", CBPago.Text)
Cmd.Parameters.AddWithValue("@Cantidad", CInt(DGV.CurrentRow.Cells(2).Value))
Cmd.Parameters.AddWithValue("@Codigo", DGV.CurrentRow.Cells(0).Value)
Try
Cmd.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Next
Ese sería mi codigo, sucede que cuando agrego 2 objetos al DataGridView, se duplican, si agrego 8, se duplica lo mismo 8 veces..
Este es el codigo para añadir los productos al DGV:
Dim Cmd2 As New OleDbCommand("Select Codigo from Productos where Nombre = @Producto", ConBD)
Cmd2.Parameters.AddWithValue("@Producto", CBP.Text)
Try
Codigo = Cmd2.ExecuteScalar()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Dim PTotal = Precio * TxtCantidad.Text
DGV.Rows.Add(Codigo, CBP.Text, TxtCantidad.Text, PTotal, Precio)
No estás utilizando la gavriable del For, solo utilizas "CurrentRow".
Cmd.Parameters.AddWithValue("@Producto", DGV.CurrentRow.Cells(1).Value)
Prueba a cambiarlo por:
Cmd.Parameters.AddWithValue("@Producto", Row.Cells(1).Value)
Cámbialo en todos los casos dentro del for, eso sí.
Muchas Gracias :D.
había intentado ponerle un Row, pero lo ponía como DGV.Row.Cell.