C#
public JsonResult UpdateInvoiceSale(tblSale sale, List salesDetail, List 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);
}
Vishal JoshiRank #377 · 6.7K reputationPosted Dec 12, 2023, 2:27 AM EST
Hello,
You need to fetch data from database and then assign updated values and save data. please check the below code.
public JsonResult UpdateInvoiceSale(tblSale sale, List salesDetail, List 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(item); retMessage.Message = "Save successfully!"; } } var tblsale = db.tblsale.where(a => a.SalesId = sale.SalesId).FirstOrDefault(); tblsale = sale; db.Entry(tblsale).State = EntityState.Modified; db.SaveChanges(); } catch (Exception ex) { retMessage.IsSuccess = false; } return Json(retMessage, JsonRequestBehavior.AllowGet); }Thanks
Vishal Joshi
Jayraj ChhayaRank #304 · 8.2K reputationPosted Dec 11, 2023, 9:33 AM EST
Problem
I have identified a bug that needs to be addressed. The bug is related to the update of the sales details. When an existing sales detail is being updated, the code correctly sets the state of the
itementity toEntityState.Modified. However, the code is missing the necessary line to update the corresponding sales details in the database.Cause
The cause of the bug is the missing line of code that updates the sales detail entity in the database when it is being modified.
Solution
To fix the bug, we need to add the line of code that updates the sales detail entity in the database when it is being modified. We can achieve this by adding the following line of code after setting the state of the
itementity toEntityState.Modified:The updated code should look like this:
foreach (var item in salesDetail) { if (item.SalesDetailId > 0) { db.Entry(item).State = EntityState.Modified; retMessage.Message = "Update successfully!"; } else { // Existing code... db.Entry(prd).State = EntityState.Modified; db.tblSalesDetails.Add(tblSaleDetail); retMessage.Message = "Save successfully!"; } }With this fix, the sales detail entity will be correctly updated in the database when it is being modified.
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Dec 12, 2023, 6:05 AM EST
Thank you team, it is working now perfectly