Ejemplo n.º 1
0
        public void NuevoVenta(DetallleCE detallleCE)
        {
            //instanciar conexion
            ConexionCD con = new ConexionCD();
            //crear el objeto SQL CONNEction
            SqlConnection cn = con.ConectarSQL();

            //Abrir la conexion
            cn.Open();

            //Inicia el control de transacciones
            using (SqlTransaction tr = cn.BeginTransaction(IsolationLevel.ReadCommitted))
            {
                //Crear un comando
                SqlCommand cmd = cn.CreateCommand();
                //Tipo de comando
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "insert into detalle(idVenta,idProducto,cantidad) values (@idVenta,@idProducto,@cantidad)";
                //Vincular el controolde transacciones con el comando

                //****************//*******/**********//
                cmd.Transaction = tr;
                //***************//*****************//

                //Asignar valores a lso sagrados
                cmd.Parameters.AddWithValue("@idVenta", detallleCE.idVenta);
                cmd.Parameters.AddWithValue("@idProducto", detallleCE.idProducto);
                cmd.Parameters.AddWithValue("@cantidad", detallleCE.cantidad);

                try
                {
                    cmd.ExecuteNonQuery();
                    //Al confirmar la transaccion
                    tr.Commit();
                }
                catch
                {
                    //Al existit error en la transaccion
                    tr.Rollback();
                }
            }
        }
        private void btnGuardarVenta_Click(object sender, EventArgs e)
        {
            VentaCE ventaCE = new VentaCE();

            ventaCE.fecventa  = Convert.ToDateTime(txtFechaVenta.Text);
            ventaCE.idCliente = Convert.ToInt32(txtIdCliente.Text);

            VentaCN ventaCN   = new VentaCN();
            int     miIdVenta = ventaCN.Nuevo(ventaCE);
            //Registrando Detalle
            DetallleCE detalleCE = new DetallleCE();
            DetalleCN  detalleCN = new DetalleCN();

            foreach (DataGridViewRow row in dgvDetalle.Rows)
            {
                detalleCE.idVenta    = miIdVenta;
                detalleCE.idProducto = Convert.ToInt32(row.Cells["idProducto"].Value);
                detalleCE.cantidad   = Convert.ToInt32(row.Cells["cantidadProducto"].Value);
                detalleCN.Nuevo(detalleCE);
            }

            MessageBox.Show("La venta y su detalle ha sido almacenado");
        }
Ejemplo n.º 3
0
 public void Nuevo(DetallleCE detalleCE)
 {
     detalleCD.NuevoVenta(detalleCE);
 }