示例#1
0
        public static void InsertarReparacion(Reparacion reparacion, List<DetalleReparacion> listaDetalles)
        {
            string conexionCadena = ConfigurationManager.ConnectionStrings["FOCAdbstring"].ConnectionString;
            SqlConnection connection = new SqlConnection();
            SqlTransaction transaction = null;
            try
            {
                connection.ConnectionString = conexionCadena;
                connection.Open();
                transaction = connection.BeginTransaction();
                string sql = "INSERT INTO REPARACIONES (fechaReparacion, fechaDevolucion, descripcion, equipoAReparar, estado, cliente, total)  VALUES (@FechaRep, @FechaDev, @Desc, @Equipo, @Estado, @Cliente, @Total); SELECT @@Identity as ID;";
                SqlCommand comand = new SqlCommand();
                comand.CommandText = sql;
                comand.Connection = connection;
                comand.Transaction = transaction;
                comand.Parameters.AddWithValue("@FechaRep", reparacion.fechaReparacion);
                comand.Parameters.AddWithValue("@FechaDev", reparacion.fechaDevolucion);
                comand.Parameters.AddWithValue("@Desc", reparacion.descripcionReparacion);
                comand.Parameters.AddWithValue("@Equipo", reparacion.equipo);
                comand.Parameters.AddWithValue("@Estado", reparacion.estado);
                comand.Parameters.AddWithValue("@Cliente", reparacion.cliente);
                comand.Parameters.AddWithValue("@Total", reparacion.total);

                int idRepracion = Convert.ToInt32(comand.ExecuteScalar());

                //DETALLES INICIO

                foreach (DetalleReparacion detallei in listaDetalles)
                {
                    string sqlDetalle = "INSERT INTO DETALLE_REPARACION VALUES (@IDMaestro, @Problema, @SubTotal)";
                    SqlCommand comand3 = new SqlCommand();
                    comand3.CommandText = sqlDetalle;
                    comand3.Connection = connection;
                    comand3.Transaction = transaction;
                    comand3.Parameters.AddWithValue("@IDMaestro", idRepracion);
                    comand3.Parameters.AddWithValue("@Problema", detallei.problema);
                    comand3.Parameters.AddWithValue("@SubTotal", detallei.subTotal);
                    comand3.ExecuteNonQuery();
                }

                //DETALLES FIN

                sql = "Insert into AUDITORIA (fecha, descripcion) values (GETDATE(),@descripcion)";
                SqlCommand comand2 = new SqlCommand();
                comand2.CommandText = sql;
                comand2.Connection = connection;
                comand2.Transaction = transaction;
                comand2.Parameters.AddWithValue("@descripcion", "Se grabó la reparacion con ID:" + idRepracion.ToString());
                comand2.ExecuteNonQuery();

                transaction.Commit(); //confirmo los cambios
            }
            catch (SqlException ex)
            {
                if (connection.State == ConnectionState.Open)
                    transaction.Rollback(); //Vuelvo atras los cambios
                throw new ApplicationException("Error al guardar la reparacion.");
            }
            finally
            {
                if (connection.State == ConnectionState.Open)
                    connection.Close();
            }
        }
        protected void Unnamed5_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    //MAESTRO
                    Reparacion rep = new Reparacion();

                    string contieneEstado = ddlEstados.SelectedValue;
                    DateTime fecha = DateTime.Parse(txtFechaRepracion.Text);
                    rep.fechaReparacion = fecha.ToString("yyyy-MM-dd");
                    fecha = DateTime.Parse(txtFechaRepracion.Text);
                    rep.fechaDevolucion = fecha.ToString("yyyy-MM-dd");
                    rep.descripcionReparacion = txtDescripcion.Text;
                    rep.equipo = txtEquipo.Text;
                    rep.estado = int.Parse(ddlEstados.SelectedValue);
                    rep.cliente = int.Parse(ddlClientes.SelectedValue);
                    rep.total = float.Parse(lblTotal.Text);

                    //DETALLE
                    List<DetalleReparacion> listaDetalles = new List<DetalleReparacion>();
                    int rowIndex = 0;

                    DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
                    if (dtCurrentTable.Rows.Count > 0)
                    {
                        for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                        {
                            DropDownList cboProblemas = (DropDownList)gvDetalleReparacion.Rows[rowIndex].Cells[0].FindControl("cboProblemas");
                            TextBox txtPrecio = (TextBox)gvDetalleReparacion.Rows[rowIndex].Cells[1].FindControl("txtPrecio");
                            DetalleReparacion detalle = new DetalleReparacion();
                            detalle.problema = int.Parse(cboProblemas.SelectedValue);
                            detalle.subTotal = float.Parse(txtPrecio.Text);

                            listaDetalles.Add(detalle);
                            rowIndex++;
                        }
                    }

                    GestorReparaciones.InsertarReparacion(rep, listaDetalles);
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('La reparación se registró correctamente')", true);
                    limpiarCampos();
                }
                catch(Exception ex)
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('La reparación no se puedo registrar')", true);
                }

            }
        }