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 JoshiPosted Dec 12, 2023, 7:31 AM
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
Emmmanuel FIADUFEPosted Dec 12, 2023, 11:01 AM
Thank you team, things are working now, due to key duplication
Jayraj ChhayaPosted Dec 11, 2023, 1:54 PM
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: