Hello Team,
I have CreatedBy column in sales table which suppose to save userId in CreatedBy column anytime data save but is not working
public JsonResult SaveInvoiceSale(tblSale sale, List
{
MasterMVCPOS.Helper.AppHelper.ReturnMessage retMessage = new AppHelper.ReturnMessage();
ASPNETMASTERPOSTEntities db = new ASPNETMASTERPOSTEntities();
retMessage.IsSuccess = true;
foreach (var item in salesDetail)
{
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.tblSales.Add(sale);
retMessage.Message = "Save successfully!";
try
{
db.SaveChanges();
retMessage.Data = Convert.ToString(sale.SalesId);
}
catch (Exception)
{
retMessage.IsSuccess = false;
}
return Json(retMessage, JsonRequestBehavior.AllowGet);
}

Vishal JoshiRank #377 · 6.7K reputationPosted Dec 12, 2023, 2:31 AM EST
Hello
The code looks good for saving the data. you might need to check the class definition and property declaration. try to add a debugger and check the detail error.
I think when you have added a new property called CreatedBy it got duplicated in the class definition and now it's stopped working. It might be missed when updating the entity framework.
Thanks
Vishal Joshi
Jayraj ChhayaRank #304 · 8.2K reputationPosted Dec 11, 2023, 8:54 AM EST
Problem
The issue reported is that the
CreatedBycolumn in thetblSaletable is not being populated with theuserIdwhen data is saved.Cause
I noticed that no code snippet assigns the
userIdto theCreatedBycolumn in thetblSaletable. This is the reason why the column remains empty.Solution
To fix this issue, you need to modify the code to assign the
userIdto theCreatedBycolumn in thetblSaletable. Assuming you have access to theuserIdvalue, you can add the following line of code before saving thesaleobject:Make sure to replace
userIdwith the actual value of the user ID.Here's the updated code:
public JsonResult SaveInvoiceSale(tblSale sale, List salesDetail) { MasterMVCPOS.Helper.AppHelper.ReturnMessage retMessage = new AppHelper.ReturnMessage(); ASPNETMASTERPOSTEntities db = new ASPNETMASTERPOSTEntities(); retMessage.IsSuccess = true; foreach (var item in salesDetail) { 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; } // Assign the userId to the CreatedBy column sale.CreatedBy = userId; db.tblSales.Add(sale); retMessage.Message = "Save successfully!"; try { db.SaveChanges(); retMessage.Data = Convert.ToString(sale.SalesId); } catch (Exception) { retMessage.IsSuccess = false; } return Json(retMessage, JsonRequestBehavior.AllowGet); }Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Dec 12, 2023, 6:01 AM EST
Thank you team, things are working now, due to key duplication