i have a problem with my code, when execute this save in database 2 records. I want save all data of Datagrid in my table called "tblDetallePresupuesto" but always save 2 times the same data.
Private Sub AgregarDetalle()
' Agregar Detalle del Presupuesto a la tabla secundaria
AbrirConexion()
Dim cmd2 As New OleDb.OleDbCommand("INSERT INTO tblDetallePresupuesto (Numero, Cantidad, Descripcion, PrecioUnitario, PrecioFinal) values(@Numero, @Cantidad, @Descripcion, @PrecioUnitario, @PrecioFinal)", Conexion)
' Contar filas y agregar valor a las variables
For i = 0 To dtgProductos.Rows.Count - 1
detalleCantidad += dtgProductos.Rows(i).Cells(0).Value
detalleDetalle += dtgProductos.Rows(i).Cells(1).Value
detalleUnitario += dtgProductos.Rows(i).Cells(2).Value
detalleSubtotal += dtgProductos.Rows(i).Cells(3).Value
cmd2.Parameters.Add("@Numero", OleDb.OleDbType.Double).Value = detalleNumero
cmd2.Parameters.Add("@Cantidad", OleDb.OleDbType.Integer).Value = detalleCantidad
cmd2.Parameters.Add("@Descripcion", OleDb.OleDbType.VarChar).Value = detalleDetalle
cmd2.Parameters.Add("@PrecioUnitario", OleDb.OleDbType.Integer).Value = detalleUnitario
cmd2.Parameters.Add("@PrecioFinal", OleDb.OleDbType.Integer).Value = detalleSubtotal
cmd2.ExecuteNonQuery()
Next
Conexion.Close()
End Sub
The function is called when i click in a button and the Datagrid have 4 columns (Cantidad, Descripcion, Unitario, Subtotal). The variable "detalleNumero" generate random number to store in db. All fields are stored with same number.
Thanks!