private void Refrescar() { using (DBVentaContainer db = new DBVentaContainer()) { dgvListaVentas.DataSource = db.Venta.Select(d => new { d.IdVenta, d.Codigo, d.FechaVenta }).ToList(); } }
private void BtnGenerarCompra_Click(object sender, EventArgs e) { using (DBVentaContainer db = new DBVentaContainer()) { using (var transaccion = db.Database.BeginTransaction()) { try { Compra oCompra = new Compra(); oCompra.Codigo = TxtCodigoVenta.Text.Trim(); oCompra.FechaCompra = DateTime.Now; oCompra.EmpleadoIdEmpleado = int.Parse(TxtIdEmpleado.Text.ToString().Trim()); oCompra.ProveedorIdProveedor = int.Parse(txtIdProveedor.Text.ToString().Trim()); db.Compra.Add(oCompra); db.SaveChanges(); foreach (DataGridViewRow item in DgvDetalleCompra.Rows) { DetalleCompra oDetalleCompra = new DetalleCompra(); oDetalleCompra.Precio = decimal.Parse(item.Cells[1].Value.ToString()); oDetalleCompra.Cantidad = int.Parse(item.Cells[2].Value.ToString()); //oDetalleVenta.IdDetalleVenta = int.Parse(item.Cells[0].Value.ToString()); oDetalleCompra.ProductoIdProducto = int.Parse(item.Cells[4].Value.ToString()); //oDetalleVenta.Importe = decimal.Parse(item.Cells[3].Value.ToString()); oDetalleCompra.CompraIdCompra = oCompra.IdCompra; db.DetalleCompra.Add(oDetalleCompra); } //DetalleVenta detalleVenta = new DetalleVenta(); //detalleVenta.ProductoIdProducto = int.Parse(TxtIdProducto.Text.ToString()); //db.DetalleVenta.Add(detalleVenta); db.SaveChanges(); transaccion.Commit(); transaccion.Dispose(); this.Close(); TxtCodgoEmpleado.Text = ""; TxtCodigoCliente.Text = ""; txtIdProveedor.Text = ""; TxtIdEmpleado.Text = ""; TxtCodigoVenta.Text = ""; TxtCodgoEmpleado.Text = ""; TxtCodigoCliente.Text = ""; TxtNombreCliente.Text = ""; TxtNombreEmpleado.Text = ""; var reporte = new Reportes.frmReporteUltimaCompra(); reporte.ShowDialog(); } catch (Exception ex) { transaccion.Rollback(); //regresar la base de datos como estaba } } } }