public IHttpActionResult PutContact(long id, Contact contact) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != contact.Id) { return(BadRequest()); } db.Entry(contact).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!ContactExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public ActionResult Create([Bind(Include = "Id,FirstName,LastName,Email,PhonePrimary,PhoneSecondary,Birthday,StreetAddress1,StreetAddress2,City,State,Zip")] Contact contact) { if (ModelState.IsValid) { db.Contacts.Add(contact); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(contact)); }
public bool UpdateUser(string userID, string pass, string cName, string phone, string tel, string gender, string birth) { try { // Query the database for the row to be updated. var query = (from mem in practiceEntities.Members where mem.Account == userID select mem).ToList(); // Execute the query, and change the column values // you want to change. foreach (Member mem in query) { mem.Password = pass; mem.Name = cName; mem.Phone = phone; mem.Tel = tel; mem.Gender = gender; mem.Birthday = DateTime.Parse(birth); } practiceEntities.SaveChanges(); return(true); } catch (DbEntityValidationException ex) { var entityError = ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage); return(false); } }
// PUT: api/author/5 public HttpResponseMessage Put(int id, AuthorInfo b) { try { using (PracticeEntities x = new PracticeEntities()) { var y = x.AuthorInfoes.FirstOrDefault(e => e.AID == id); if (y == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Author with Id=" + id.ToString() + " not found")); } else { y.AuthorName = b.AuthorName; y.City = b.City; x.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK, y)); } } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public HttpResponseMessage Delete(int id) { try { using (PracticeEntities entities = new PracticeEntities()) { var entity = entities.Employees.FirstOrDefault(emp => emp.employee_id == id); if (entity != null) { entities.Employees.Remove(entity); entities.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK)); } else { return(Request.CreateResponse(HttpStatusCode.NotFound, "Employee with id" + id.ToString() + "not found")); } } } catch (Exception ex) { return(Request.CreateResponse(HttpStatusCode.BadRequest, ex)); } }
public HttpResponseMessage Put(int id, [FromBody] Employee employee) { try { using (PracticeEntities entities = new PracticeEntities()) { var entity = entities.Employees.FirstOrDefault(emp => emp.employee_id == id); if (entity != null) { entity.months = employee.months; entity.name = employee.name; entity.salary = employee.salary; entities.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK)); } else { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with id" + id.ToString() + "not found")); } } } catch (Exception ex) { return(Request.CreateResponse(HttpStatusCode.BadRequest, ex)); } }
// POST: api/books public void Post(AuthorInfo b) { using (PracticeEntities x = new PracticeEntities()) { x.AuthorInfoes.Add(b); x.SaveChanges(); } }
public ActionResult Edit([Bind(Include = "Id,FirstName,LastName,Email,PhonePrimary,PhoneSecondary,Birthday,StreetAddress1,StreetAddress2,City,State,Zip")] Contact contact) { using (PracticeEntities _context = new PracticeEntities()) { if (ModelState.IsValid) { _context.Entry(contact).State = EntityState.Modified; _context.SaveChanges(); return(RedirectToAction("Index")); } return(View(contact)); } }
public JsonResult DeleteContact(int?ID) { using (PracticeEntities _context = new PracticeEntities()) { var contact = _context.Contacts.Find(ID); if (ID == null) { return(Json(data: "Not Deleted", behavior: JsonRequestBehavior.AllowGet)); } _context.Contacts.Remove(contact); _context.SaveChanges(); return(Json(data: "Deleted", behavior: JsonRequestBehavior.AllowGet)); } }
public void Addcustomer(int id, int age, decimal salary, string address, string name) { var dbContaxt = new PracticeEntities(); CUSTOMER cust = new CUSTOMER(); cust.ID = practice.CUSTOMERS.Max(c => c.ID) + 1; cust.ADDRESS = address; cust.SALARY = salary; cust.NAME = name; cust.AGE = age; dbContaxt.CUSTOMERS.Add(cust); dbContaxt.SaveChanges(); // return cust(); }
public HttpResponseMessage Post([FromBody] Employee emp) { try { using (PracticeEntities entities = new PracticeEntities()) { entities.Employees.Add(emp); entities.SaveChanges(); } var message = Request.CreateResponse(HttpStatusCode.Created, emp); message.Headers.Location = new Uri(Request.RequestUri + emp.employee_id.ToString()); return(message); } catch (Exception ex) { return(Request.CreateResponse(HttpStatusCode.BadRequest, ex)); } }
public HttpResponseMessage Delete(int id) { try { using (PracticeEntities pe = new PracticeEntities()) { var entity = pe.Employees.FirstOrDefault(x => x.ID == id); if (entity == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, $"The employee with ID = {id} not found to delete")); } pe.Employees.Remove(entity); pe.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK)); } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public HttpResponseMessage Put(int id, [FromBody] Employee employee) { try { using (PracticeEntities pe = new PracticeEntities()) { var entity = pe.Employees.FirstOrDefault(x => x.ID == id); if (entity == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, $"The employee with ID = {id} could not be found to update!")); } entity.FirstName = employee.FirstName; entity.LastName = employee.LastName; entity.Gender = employee.Gender; entity.Salary = employee.Salary; pe.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK)); } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
// DELETE: api/books/5 public HttpResponseMessage Delete(int id) { try { using (PracticeEntities x = new PracticeEntities()) { var y = x.BookInfoes.FirstOrDefault(e => e.ID == id); if (y == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Book with Id=" + id.ToString() + " not found")); } else { x.BookInfoes.Remove(x.BookInfoes.FirstOrDefault(e => e.ID == id)); x.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK, y)); } } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
/// <summary> /// Save method. /// </summary> public void Save() { try { _context.SaveChanges(); } catch (DbEntityValidationException e) { var outputLines = new List <string>(); foreach (var eve in e.EntityValidationErrors) { outputLines.Add(string.Format( "{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:", DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State)); foreach (var ve in eve.ValidationErrors) { outputLines.Add(string.Format("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage)); } } System.IO.File.AppendAllLines(@"C:\errors.txt", outputLines); throw e; } }
public ActionResult Add(my_table m) { pe.Entry(m).State = System.Data.EntityState.Added; pe.SaveChanges(); return(RedirectToAction("Index")); }