public async Task <ActionResult> Edit(int?id, HttpPostedFileBase upload) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } var user = db.Users.Find(id); if (TryUpdateModel(user, "", new string[] { "user1", "email", "password", "confirmPassword", "nombreFichero" })) { try { if (upload != null && upload.ContentLength > 0) { if (user.Files.Any(fi => fi.FileType == (int?)FileType.Image)) { db.Files.Remove(user.Files.First(fi => fi.FileType == (int?)FileType.Image)); } if (user.nombreFichero.Equals("")) { user.nombreFichero = upload.FileName; } var imagen = new Models.File { FileName = System.IO.Path.GetFileName(upload.FileName), FileType = (int?)FileType.Image, ContentType = upload.ContentType }; using (var reader = new BinaryReader(upload.InputStream)) { imagen.Content = reader.ReadBytes(upload.ContentLength); user.foto = imagen.Content; } user.Files = new List <Models.File> { imagen }; } db.Entry(user).State = EntityState.Modified; Debug.WriteLine(user.ToString()); await db.SaveChangesAsync(); return(RedirectToAction("Index", "Home", new { id = user.Id })); } catch (RetryLimitExceededException /* dex */) { //Log the error (uncomment dex variable name and add a line here to write a log. ModelState.AddModelError("No ha sido posible añadir el usuario", " Inténtalo de nuevo, si el problema persisite contacte con el administrador."); } catch (System.Data.Entity.Infrastructure.DbUpdateException e) { ViewBag.ErrorMessage = "Ya existe " + valorDuplicado(e); } catch (System.Data.Entity.Validation.DbEntityValidationException e) { Debug.WriteLine(e.EntityValidationErrors + "***"); } } ViewBag.UserId = new SelectList(db.Users, "Id", "user1", id); return(View(user)); }
private void btnUpdate_Click(object sender, EventArgs e) { if (checkBoxNome.Checked) { try { using (var maqContext = new MaquinaContext(_DB)) { var item = maqContext.Maquinas.FirstOrDefault(c => c.Ip == textBoxIp.Text.ToString()); if (item != null) { item.MudarNome(textBoxNome.Text.ToString()); maqContext.Entry(item).State = System.Data.Entity.EntityState.Modified; maqContext.SaveChanges(); MessageBox.Show("Realizado com sucesso!"); } } } catch (Exception ex) { MessageBox.Show(ex.Message); MessageBox.Show(ex.StackTrace); } } if (checkBoxLab.Checked) { try { int id; using (var labContext = new LaboratorioContext(_DB)) { id = labContext.Laboratorios.Where(s => s.Nome.Contains(textBoxLab.Text.ToString())).FirstOrDefault().Id; } using (var maqContext = new MaquinaContext(_DB)) { var item = maqContext.Maquinas.FirstOrDefault(c => c.Ip == textBoxIp.Text.ToString()); if (item != null) { item.MudarIdLab(id); maqContext.Entry(item).State = System.Data.Entity.EntityState.Modified; maqContext.SaveChanges(); MessageBox.Show("Realizado com sucesso!"); } } } catch (Exception ex) { MessageBox.Show(ex.Message); MessageBox.Show(ex.StackTrace); } } if (checkBoxIp.Checked) { try { using (var maqContext = new MaquinaContext(_DB)) { var item = maqContext.Maquinas.FirstOrDefault(c => c.Nome == textBoxNome.Text.ToString()); if (item != null) { item.MudarIp(textBoxIp.Text.ToString()); maqContext.Entry(item).State = System.Data.Entity.EntityState.Modified; maqContext.SaveChanges(); MessageBox.Show("Realizado com sucesso!"); } } } catch (Exception ex) { MessageBox.Show(ex.Message); MessageBox.Show(ex.StackTrace); } } }