Emmmanuel FIADUFE

Emmmanuel FIADUFE

  • 784
  • 1.1k
  • 55.3k

Update query fail

Dec 11 2023 2:15 PM
public JsonResult UpdateInvoiceSale(tblSale sale, List<tblSalesDetail> salesDetail, List<int> deleted)
{
    MasterMVCPOS.Helper.AppHelper.ReturnMessage retMessage = new AppHelper.ReturnMessage();
    ASPNETMASTERPOSTEntities db = new ASPNETMASTERPOSTEntities();
    retMessage.IsSuccess = true;

    try
    {
        if (deleted != null)
        {
            foreach (var item in deleted)
            {
                var data = db.tblSalesDetails.Where(x => x.SalesDetailId == item).FirstOrDefault();
                db.tblSalesDetails.Remove(data);
            }
        }

        foreach (var item in salesDetail)
        {
            if (item.SalesDetailId > 0)
            {
                db.Entry(item).State = EntityState.Modified;
                retMessage.Message = "Update successfully!";
            }
            else
            {
                //sale.tblSalesDetails.Add(new tblSalesDetail { ProductId = item.ProductId, UnitPrice = item.UnitPrice, Quantity = item.Quantity, LineTotal = item.LineTotal });
                var prd = db.tblProductStocks.Where(x => x.ProductId == item.ProductId && x.Quantity > 0).FirstOrDefault();
                prd.Quantity = prd.Quantity - item.Quantity;
                db.Entry(prd).State = EntityState.Modified;
                
                db.tblSalesDetails.Add(tblSaleDetail);
                retMessage.Message = "Save successfully!";
            }
        }

        //tblsale = sale;
        db.Entry(tblsale).State = EntityState.Modified;
        db.SaveChanges();
    }
    catch (Exception ex)
    {
        retMessage.IsSuccess = false;
    }

    return Json(retMessage, JsonRequestBehavior.AllowGet);
}

 


Answers (3)