Sie sind auf Seite 1von 2

Function GuardarDataGrid() As Boolean

Dim Conn As New OleDbConnection("Aquí tu cadena de conexion")

Try

Conn.Open()

For X As Integer = 0 To DataGridView1.Rows.Count - 1

Using cmdInsert As New OleDbCommand

With cmdInsert

.CommandText = " INSERT INTO DetallesFactura (IdFactura,

Cantidad, Codigo, Detalle, Descuento, Precio, Importe) VALUES

(@IdFactura,@Cantidad,@Codigo,@Detalle,@Descuento,@Precio,@Importes)"

.CommandType = CommandType.Text

.Connection = Conn

.Parameters.Add("@IdFactura", OleDbType.Integer).Value =

Convert.ToInt32(DataGridView1.Rows(X).Cells("IdFactura").Value)

.Parameters.Add("@Cantidad", OleDbType.Integer).Value =

Convert.ToInt32(DataGridView1.Rows(X).Cells("Cantidad").Value)

.Parameters.Add("@Codigo", OleDbType.VarChar).Value =

DataGridView1.Rows(X).Cells("Codigo").Value.ToString

.Parameters.Add("@Detalle", OleDbType.VarChar).Value =

DataGridView1.Rows(X).Cells("Detalle").Value.ToString

.Parameters.Add("@Descuento", OleDbType.Double).Value =

Convert.ToDouble(DataGridView1.Rows(X).Cells("Descuento").Value)

.Parameters.Add("@Precio", OleDbType.Double).Value =

Convert.ToDouble(DataGridView1.Rows(X).Cells("Precio").Value)
.Parameters.Add("@Importes", OleDbType.Double).Value =

Convert.ToDouble(DataGridView1.Rows(X).Cells("Importes").Value)

End With

Dim Correcto As Integer = cmdInsert.ExecuteNonQuery

If Correcto = 0 Then

MessageBox.Show("Error no se pudo agregar la fila",

"Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Error)

End If

End Using

Next
Catch ex As Exception
MessageBox.Show(ex.Message, "Advertencia", MessageBoxButtons.OK,

MessageBoxIcon.Error)

Finally

If Conn.State = ConnectionState.Open Then

Conn.Close()

Conn.Dispose()

End If

End Try

Return True

End Function

Das könnte Ihnen auch gefallen