Example #1
0
        public ActionResult postEdit()
        {
            HttpPostedFileBase f = Request.Files.Get("img_file");
            int         id       = Int32.Parse(Request.Form.Get("id"));
            tbl_Product p        = db.tbl_Product.Find(id);

            if (f != null && f.ContentLength > 0)
            {
                try
                {
                    string oldPath = Path.Combine(Server.MapPath("~/Content/dailyShop/img/product"),
                                                  Path.GetFileName(p.img));
                    FileInfo oldf = new FileInfo(oldPath);
                    if (oldf.Exists)
                    {
                        oldf.Delete();
                    }
                    string path = Path.Combine(Server.MapPath("~/Content/dailyShop/img/product"),
                                               Path.GetFileName(f.FileName));
                    f.SaveAs(path);
                    p.name            = Request.Form.Get("name");
                    p.cate_id         = Int32.Parse(Request.Form.Get("cate_id"));
                    p.meta_name       = Request.Form.Get("meta_name");
                    p.img             = f.FileName;
                    p.rate            = Byte.Parse(Request.Form.Get("rate"));
                    p.amount          = Int32.Parse(Request.Form.Get("amount"));
                    p.price           = Int32.Parse(Request.Form.Get("price"));
                    p.discount        = Int32.Parse(Request.Form.Get("discount"));
                    p.get_at          = DateTime.Parse(Request.Form.Get("get_at"));
                    db.Entry(p).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }
                catch (Exception ex)
                {
                }
            }
            else
            {
                p.name      = Request.Form.Get("name");
                p.cate_id   = Int32.Parse(Request.Form.Get("cate_id"));
                p.meta_name = Request.Form.Get("meta_name");
                p.rate      = Byte.Parse(Request.Form.Get("rate"));
                p.amount    = Int32.Parse(Request.Form.Get("amount"));
                p.price     = Int32.Parse(Request.Form.Get("price"));
                p.discount  = Int32.Parse(Request.Form.Get("discount"));
                p.get_at    = DateTime.Parse(Request.Form.Get("get_at"));
            }
            db.Entry(p).State = System.Data.Entity.EntityState.Modified;
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
Example #2
0
        //Save the data in database either on creating or editing Product
        public JsonResult Create(ProductViewModel model)
        {
            var result = false;

            try
            {
                Product pro = new Product();
                pro.Id    = model.Id;
                pro.PName = model.PName;
                pro.Price = model.Price;
                if (model.Id == 0)
                {
                    db.Products.Add(pro);
                    db.SaveChanges();
                    result = true;
                }
                else
                {
                    db.Entry(pro).State = EntityState.Modified;
                    db.SaveChanges();
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(Json(result, JsonRequestBehavior.AllowGet));
        }
        public IHttpActionResult PutItem(int id, Item item)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != item.ItemID)
            {
                return(BadRequest());
            }

            db.Entry(item).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ItemExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public IHttpActionResult PutCustomer(int id, Customer customer)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != customer.CustomerID)
            {
                return(BadRequest());
            }

            db.Entry(customer).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CustomerExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Example #5
0
 /// <summary>
 /// 執行新增或修改
 /// </summary>
 public void SaveOrInsert()
 {
     using (DemoEntities DemoEntities = new DemoEntities())
     {
         //將ViewModel 轉換成 EntityModel
         Employee EEmployee = Mapper.Map <EmployeeEditViewModel, Employee>(this);
         //如果有 EmployeeID 表示修改
         if (this.EmployeeID != 0)
         {
             DemoEntities.Entry(EEmployee).State = System.Data.Entity.EntityState.Modified;
             DemoEntities.SaveChanges();
         }
         else
         {
             //沒有 EmployeeID 表示新增
             try
             {
                 DemoEntities.Employee.Add(EEmployee);
                 DemoEntities.SaveChanges();
             }
             catch (Exception ex)
             {
                 throw;
             }
         }
     }
 }
 public ActionResult AddUser(UserMaster obj)
 {
     try
     {
         if (obj.UserId <= 0)
         {
             obj.Created = DateTime.Now;
             context.UserMasters.Add(obj);
             context.SaveChanges();
             return(Json(obj));
         }
         else
         {
             UserMaster objuser = context.UserMasters.Find(obj.UserId);
             objuser.FirstName            = obj.FirstName;
             objuser.LastName             = obj.LastName;
             objuser.UserName             = obj.UserName;
             objuser.Email                = obj.Email;
             objuser.Modified             = DateTime.Now;
             context.Entry(objuser).State = EntityState.Modified;
             context.SaveChanges();
             return(Json(objuser));
         }
     }
     catch (Exception e)
     {
         throw;
     }
 }
Example #7
0
        public IHttpActionResult PutProduct(int id, Product product)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != product.ProductID)
            {
                return(BadRequest());
            }

            db.Entry(product).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public IHttpActionResult PostOrder(Order order)
        {
            try
            {
                //Order table
                if (order.OrderID == 0)
                {
                    db.Orders.Add(order);
                }
                else
                {
                    //db.Order.Attach(order);
                    db.Entry(order).State = EntityState.Modified;
                }

                //OrderItems table
                foreach (var item in order.OrderItems)
                {
                    if (item.OrderItemID == 0)
                    {
                        db.OrderItems.Add(item);
                    }
                    else
                    {
                        db.Entry(item).State = EntityState.Modified;
                    }
                }

                //Delete for OrderItems
                if (order.DeletedOrderItemIDs != null)
                {
                    foreach (var id in order.DeletedOrderItemIDs.Split(',').Where(x => x != ""))
                    {
                        OrderItem x = db.OrderItems.Find(Convert.ToInt64(id));
                        db.OrderItems.Remove(x);
                    }
                }

                db.SaveChanges();

                return(Ok());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #9
0
 public override int Delete(BrandModel obj)
 {
     using (DemoEntities db = new DemoEntities())
     {
         db.Entry(obj).State = EntityState.Deleted;
         return(db.SaveChanges());
     }
 }
Example #10
0
 public override int Update(ProductModel obj)
 {
     using (DemoEntities db = new DemoEntities())
     {
         db.Entry(obj).State = EntityState.Modified;
         return(db.SaveChanges());
     }
 }
Example #11
0
        //Save data in database either on creating or editing Sales
        public JsonResult Create(ProductSold model)
        {
            var result = false;

            try
            {
                if (model.Id > 0)      //Updating
                {
                    ProductSold pro = db.ProductSolds.SingleOrDefault(x => x.Id == model.Id);

                    if (model.ProductId == 0)   //Changes not made for this field
                    {
                        pro.ProductId = pro.ProductId;
                    }
                    else
                    {
                        pro.ProductId = model.ProductId;
                    }
                    if (model.CustomerId == 0)
                    {
                        pro.CustomerId = pro.CustomerId;
                    }
                    else
                    {
                        pro.CustomerId = model.CustomerId;
                    }
                    if (model.StoreId == 0)
                    {
                        pro.StoreId = pro.StoreId;
                    }
                    else
                    {
                        pro.StoreId = model.StoreId;
                    }
                    pro.DateSold        = model.DateSold;
                    db.Entry(pro).State = EntityState.Modified;
                    db.SaveChanges();
                    result = true;
                }
                else     //Adding
                {
                    ProductSold pro = new ProductSold();
                    pro.ProductId  = model.ProductId;
                    pro.CustomerId = model.CustomerId;
                    pro.StoreId    = model.StoreId;
                    pro.DateSold   = model.DateSold;
                    db.ProductSolds.Add(pro);
                    db.SaveChanges();
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(Json(result, JsonRequestBehavior.AllowGet));
        }
Example #12
0
 /// <summary>
 /// 執行刪除
 /// </summary>
 public void Delete()
 {
     using (DemoEntities DemoEntities = new DemoEntities())
     {
         Employee Employee = DemoEntities.Employee.Single(x => x.EmployeeID == this.EmployeeID);
         DemoEntities.Entry(Employee).State = System.Data.Entity.EntityState.Deleted;
         DemoEntities.SaveChanges();
     }
 }
 public ActionResult Edit([Bind(Include = "EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO")] EMPDATA eMPDATA)
 {
     if (ModelState.IsValid)
     {
         db.Entry(eMPDATA).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(eMPDATA));
 }
Example #14
0
 public ActionResult Edit([Bind(Include = "ChequeID,Bank,Branch,Cheque_No,Amount")] cheque cheque)
 {
     if (ModelState.IsValid)
     {
         db.Entry(cheque).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(cheque));
 }
 public ActionResult Edit([Bind(Include = "DepartmentID,Name")] Department department)
 {
     if (ModelState.IsValid)
     {
         db.Entry(department).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(department));
 }
Example #16
0
 public ActionResult Edit([Bind(Include = "id,name,age")] Client client)
 {
     if (ModelState.IsValid)
     {
         db.Entry(client).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(client));
 }
Example #17
0
 public ActionResult Edit([Bind(Include = "EmployeeID,Name,DepartmentID,Address")] Employee employee)
 {
     if (ModelState.IsValid)
     {
         db.Entry(employee).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.DepartmentID = new SelectList(db.Departments, "DepartmentID", "Name", employee.DepartmentID);
     return(View(employee));
 }
Example #18
0
        public void NoTracking()
        {
            using (var db = new DemoEntities())
            {
                var category = db.Categories.AsNoTracking()
                               .Where(x => x.CategoryId == 1)
                               .SingleOrDefault();

                var categoryEntry = db.Entry(category);
            }
        }
Example #19
0
 public void UpdateStudent(Student student)
 {
     EntityFramework.Student efStudent = _context.Students.FirstOrDefault(s => s.Id == student.Id);
     if (efStudent != null)
     {
         efStudent.FirstName             = student.FirstName;
         efStudent.LastName              = student.LastName;
         _context.Entry(efStudent).State = EntityState.Modified;
         Save();
     }
 }
Example #20
0
 public ActionResult Edit(KichCo model)
 {
     if (ModelState.IsValid)
     {
         db.Entry(model).State = System.Data.Entity.EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("List"));
     }
     else
     {
         return(View(model));
     }
 }
Example #21
0
 public async Task UpdateAsync([FromBody] InforMation employee)
 {
     try
     {
         var db = new DemoEntities();
         db.Entry(employee).State = EntityState.Modified;
         await db.SaveChangesAsync();
     }
     catch
     {
         throw;
     }
 }
Example #22
0
 //更新圖檔
 public bool Update(tb_image oImage)
 {
     try
     {
         _db.Entry(oImage).State = System.Data.Entity.EntityState.Modified;
         _db.SaveChanges();
         return(true);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Example #23
0
        public void ExplicitLoading()
        {
            using (var db = new DemoEntities())
            {
                Category category = db.Categories.Find(1);

                db.Entry(category)
                .Collection(x => x.Products)
                .Query()
                .Where(x => x.ProductName.Contains("Jeans"))
                .Load();
            }
        }
        //GET: Order
        public ActionResult Ordering()
        {
            CartData cart = (CartData)Session["cart"];

            if (cart.amounts == 0)
            {
                return(Redirect(Request.UrlReferrer.AbsoluteUri));
            }
            String       cus_address = Request.Form.Get("address");
            String       cus_name    = Request.Form.Get("name");
            tbl_Customer cus         = (tbl_Customer)Session["user"];
            CartData     cartList    = (CartData)Session["cart"];
            tbl_Bill     bill        = db.tbl_Bill.Create();

            bill.cus_id       = cus.id;
            bill.level_status = 0;
            bill.total_price  = cartList.cost;
            bill.cus_address  = cus_address;
            bill.cus_name     = cus_name;
            bill.created_at   = DateTime.Now;
            db.tbl_Bill.Add(bill);
            for (int i = 0; i < cartList.arrCart.Count; i++)
            {
                Cart           c          = (Cart)cartList.arrCart[i];
                tbl_BillDetail billDetail = db.tbl_BillDetail.Create();
                billDetail.bill_id = bill.id;
                billDetail.pro_id  = c.item.id;
                billDetail.price   = c.cost * c.amounts;
                billDetail.amount  = c.amounts;
                db.tbl_BillDetail.Add(billDetail);
                tbl_Product p = db.tbl_Product.Find(c.item.id);
                p.amount         -= c.amounts;
                db.Entry(p).State = System.Data.Entity.EntityState.Modified;
            }
            db.SaveChanges();
            Session.Remove("cart");
            return(RedirectToAction("Account"));
        }
 public ActionResult Edit([Bind(Include = "id,username,pass,name,email,phone,cus_address")] tbl_Customer tbl_Customer)
 {
     if (Session["admin"] == null)
     {
         return(RedirectToAction("Login"));
     }
     if (ModelState.IsValid)
     {
         db.Entry(tbl_Customer).State = System.Data.Entity.EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(tbl_Customer));
 }
Example #26
0
 public ActionResult Edit([Bind(Include = "id,name,meta_name")] tbl_Slider tbl_Slider)
 {
     if (Session["admin"] == null)
     {
         return(RedirectToAction("Login"));
     }
     if (ModelState.IsValid)
     {
         db.Entry(tbl_Slider).State = System.Data.Entity.EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(tbl_Slider));
 }
 public ActionResult Edit([Bind(Include = "id,pub_id,name,meta_name,amount")] tbl_Category tbl_Category)
 {
     if (Session["admin"] == null)
     {
         return(RedirectToAction("Login"));
     }
     if (ModelState.IsValid)
     {
         db.Entry(tbl_Category).State = System.Data.Entity.EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.pub_id = new SelectList(db.tbl_Publisher, "id", "name", tbl_Category.pub_id);
     return(View(tbl_Category));
 }
 public ActionResult Edit([Bind(Include = "id,level_status,cus_id,created_at,total_price,cus_name")] tbl_Bill tbl_Bill)
 {
     if (Session["admin"] == null)
     {
         return(RedirectToAction("Login"));
     }
     if (ModelState.IsValid)
     {
         db.Entry(tbl_Bill).State = System.Data.Entity.EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.cus_id = new SelectList(db.tbl_Customer, "id", "username", tbl_Bill.cus_id);
     return(View(tbl_Bill));
 }
 public ActionResult Edit([Bind(Include = "id,cus_id,pro_id,amount,total_price")] tbl_Cart tbl_Cart)
 {
     if (Session["admin"] == null)
     {
         return(RedirectToAction("Login"));
     }
     if (ModelState.IsValid)
     {
         db.Entry(tbl_Cart).State = System.Data.Entity.EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.cus_id = new SelectList(db.tbl_Customer, "id", "username", tbl_Cart.cus_id);
     ViewBag.pro_id = new SelectList(db.tbl_Product, "id", "name", tbl_Cart.pro_id);
     return(View(tbl_Cart));
 }
Example #30
0
 public ActionResult Edit([Bind(Include = "id,bill_id,pro_id,amount,price")] tbl_BillDetail tbl_BillDetail)
 {
     if (Session["admin"] == null)
     {
         return(RedirectToAction("Login"));
     }
     if (ModelState.IsValid)
     {
         db.Entry(tbl_BillDetail).State = System.Data.Entity.EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.bill_id = new SelectList(db.tbl_Bill, "id", "cus_name", tbl_BillDetail.bill_id);
     ViewBag.pro_id  = new SelectList(db.tbl_Product, "id", "name", tbl_BillDetail.pro_id);
     return(View(tbl_BillDetail));
 }