Problem with Datagrid and Database (For i)

Viewed 21

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!

1 Answers

Solved, i change the code but only call the function AgregarDetalle 1 time. Put all in this function and solved the problem.

    Private Sub GuardarTodo()
        ' Agregar Detalle del Presupuesto a la tabla secundaria
        Dim i As Integer
        Dim cmd2
        ' Conexión a la tabla Principal
        Dim cmd
        AbrirConexion()

        ' Agregar campos del DatagridView a la table Secundaria
        Try

            For i = 0 To dtgProductos.Rows.Count - 1
                ' Abrir dB
                ' AbrirConexion()
                ' Agregar a la tabla secundaria de tblDetallePresupuesto
                cmd2 = New OleDb.OleDbCommand("INSERT INTO tblDetallePresupuesto (Numero, Cantidad, Descripcion, PrecioUnitario, PrecioFinal) values(@Numero, @Cantidad, @Descripcion, @PrecioUnitario, @PrecioFinal)", Conexion)
                With cmd2
                    .Parameters.AddWithValue("@Numero", CInt(detalleNumero))
                    .Parameters.AddWithValue("@Cantidad", CInt(dtgProductos.Rows(i).Cells(0).Value.ToString()))
                    .Parameters.AddWithValue("@Descripcion", dtgProductos.Rows(i).Cells(1).Value.ToString())
                    .Parameters.AddWithValue("@PrecioUnitario", FormatCurrency(dtgProductos.Rows(i).Cells(2).Value.ToString()))
                    .Parameters.AddWithValue("@PrecioFinal", FormatCurrency(dtgProductos.Rows(i).Cells(3).Value.ToString()))
                    .ExecuteNonQuery()
                End With
            Next
            ' Agregar datos a la tabla Principal tblPresupuestos
            cmd = New OleDbCommand("INSERT INTO tblPresupuestos (Fecha, RazonSocial, Direccion, Telefono, Subtotal, Descuento, TotalPresupuesto, NumeroDetalle) values(@Fecha, @RazonSocial, @Direccion, @Telefono, @Subtotal, @Descuento, @TotalPresupuesto, @NumeroDetalle)", Conexion)
            With cmd
                .Parameters.AddWithValue("@Fecha", txtFecha.Text)
                .Parameters.AddWithValue("@RazonSocial", txtRazonSocial.Text)
                .Parameters.AddWithValue("@Direccion", txtDireccion.Text)
                .Parameters.AddWithValue("@Telefono", txtTelefono.Text)
                .Parameters.AddWithValue("@Subtotal", txtSubtotal.Text)
                .Parameters.AddWithValue("@Descuento", txtPesosDescuento.Text)
                .Parameters.AddWithValue("@TotalPresupuesto", txtTOTAL.Text)
                .Parameters.AddWithValue("@NumeroDetalle", detalleNumero)
                .ExecuteNonQuery()
            End With
        Catch ex As Exception
            Conexion.Close()
            MsgBox(ex.Message, vbCritical, "Error")
        End Try
        MsgBox("Guardado correctamente.", vbOKOnly + vbInformation, "Guardado")
        Conexion.Close()
        VaciarCampos()
    End Sub

All data saved in two tables in same database.

Thanks for reply.

Related