Ejemplo n.º 1
0
        public ActionResult Edit(EmpTable postedEmp)
        {
            //get the Entity FM
            var com = new EmpDataEntities();
            //Find UR EMployee
            var emp = com.EmpTables.FirstOrDefault(e => e.EmpID == postedEmp.EmpID);

            if (emp == null)
            {
                throw new Exception("Employee not found");
            }
            //SEt the new details
            emp.EmpName    = postedEmp.EmpName;
            emp.EmpAddress = postedEmp.EmpAddress;
            emp.EmpSalary  = postedEmp.EmpSalary;
            com.SaveChanges();
            //update
            return(RedirectToAction("Index"));
        }
Ejemplo n.º 2
0
        public HttpResponseMessage Post([FromBody] Emp emp)
        {
            try
            {
                using (EmpDataEntities entities = new EmpDataEntities())
                {
                    entities.Emps.Add(emp);
                    entities.SaveChanges();

                    var msg = Request.CreateResponse(HttpStatusCode.Created, emp);
                    msg.Headers.Location = new Uri(Request.RequestUri + emp.ID.ToString());
                    return(msg);
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }
Ejemplo n.º 3
0
 public HttpResponseMessage Delete(int id)
 {
     try
     {
         using (EmpDataEntities entities = new EmpDataEntities())
         {
             var entity = entities.Emps.FirstOrDefault(e => e.ID == id);
             if (entity == null)
             {
                 return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Employee with id : " + id.ToString() + "not found to delete"));
             }
             else
             {
                 entities.Emps.Remove(entity);
                 entities.SaveChanges();
                 return(Request.CreateResponse(HttpStatusCode.OK));
             }
         }
     }
     catch (Exception ex)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
     }
 }