private void BgLine_DoWork(object sender, DoWorkEventArgs e) { if (e.Argument != null) { lineas lineToOper = (lineas)e.Argument; if (lineToOper.id == 0) { //AGREGAR e.Result = daoLin.AddLine((lineas)e.Argument); } else { //EDITAR e.Result = daoLin.EditLinea((lineas)e.Argument); } } }
public int AddLine(lineas lin) { int regs = 0; try { using (var context = new MttoAppEntities()) { context.lineas.Add(new lineas() { id_planta = lin.id_planta, nombre = lin.nombre, image_path = lin.image_path }); regs = context.SaveChanges(); } } catch (Exception e) { MessageBox.Show("Excepción al agregar línea: " + e.ToString(), "Atención", MessageBoxButton.OK, MessageBoxImage.Error); } return(regs); }
public int EditLinea(lineas lin) { lineas line; int regs = 0; try { //1. Get row from DB using (var context = new MttoAppEntities()) { line = context.lineas.Where(s => s.id == lin.id).FirstOrDefault(); } //2. change data in disconnected mode (out of ctx scope) if (line != null) { line.nombre = lin.nombre; line.image_path = lin.image_path; line.id_planta = lin.id_planta; } //save modified entity using new Context using (var context = new MttoAppEntities()) { //3. Mark entity as modified context.Entry(line).State = System.Data.Entity.EntityState.Modified; //4. call SaveChanges regs = context.SaveChanges(); } } catch (Exception e) { MessageBox.Show("Excepción al editar linea: " + e.ToString(), "Atención", MessageBoxButton.OK, MessageBoxImage.Error); } return(regs); }
private void btnCrearLinea_Click(object sender, RoutedEventArgs e) { try { string tag = btnCrearLinea.Tag.ToString(); switch (tag) { case "0": EnableControls(true); btnCrearLinea.Tag = "2"; txtBtnOp.Text = "Guardar línea"; break; //PREPARAR CONTROLES PARA NUEVO REGISTRO case "1": //GUARDAR EDICIÓN if (cboPlantas.Text.Length > 0) { if (txtNombre.Text.Trim().Length > 0) { if (image.Length > 0) { lineas l = new lineas(); l.id = id_edit; l.id_planta = int.Parse(cboPlantas.SelectedValue.ToString()); l.nombre = txtNombre.Text.Trim(); l.image_path = Path.GetFileName(image); if (!bgLine.IsBusy) { splash.Show(); bgLine.RunWorkerAsync(l); } } else { MessageBox.Show("Por favor seleccione una imágen válida!"); } } else { MessageBox.Show("Por favor escribir nombre!"); } } else { MessageBox.Show("Por favor seleccione planta!"); } break; case "2": //GUARDAR NUEVA LÍNEA if (cboPlantas.Text.Length > 0) { if (txtNombre.Text.Trim().Length > 0) { if (image.Length > 0) { lineas l = new lineas(); l.id_planta = int.Parse(cboPlantas.SelectedValue.ToString()); l.nombre = txtNombre.Text.Trim(); l.image_path = Path.GetFileName(image); if (!bgLine.IsBusy) { splash.Show(); bgLine.RunWorkerAsync(l); } } else { MessageBox.Show("Por favor seleccione una imágen válida!"); } } else { MessageBox.Show("Por favor escribir nombre!"); } } else { MessageBox.Show("Por favor seleccione planta!"); } EnableControls(false); ClearControls(); break; default: break; } } catch (Exception ex) { MessageBox.Show("Error al intentar transacción. " + ex.Message); } }