Hello Team,
this my query isn't working, userId reference CreatedBy Column
public ActionResult DeleteUser(int UserId)
{
try
{
ASPNETMASTERPOSTEntities db = new ASPNETMASTERPOSTEntities();
var user = db.tblUsers.Find(UserId);
user.IsActive = false;
db.Entry(user).State = EntityState.Modified;
db.SaveChanges();
return Json(true, JsonRequestBehavior.AllowGet);
}
catch (Exception)
{
return Json(false, JsonRequestBehavior.AllowGet);
}
}
Vishal JoshiPosted Dec 12, 2023, 7:21 AM
Hello Emmanual,
Delete query may not work due to reference/foreign key to another table such as column createdby.
In this type of case, deleting is not preferable. you should use active and InActive flags to maintain delete.
once a user is deleted it should be marked as inactive. all inactive users are not visible on the list page by adding a filter on a flag.
Also, restrict inactive users in authentication and authorization.
Thanks
Vishal Joshi
Amit MohantyPosted Dec 12, 2023, 5:52 AM
Hey Emmmanuel, firstly without the error message or specific issue, it's hard to pinpoint the exact problem. Try the below code with additional error handling that might provide more insight into the issue:
Secondly, userId reference CreatedBy column means, you're looking for a user based on their CreatedBy rather than the user's own ID. If this is true then you might need to adjust the logic to find the user by the CreatedBy reference rather than the UserId. Try below code which will find the user based on CreatedBy column.