1

In my Controller I have action

[HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public async Task<ActionResult> DeleteConfirmed(int id) { clsHost HostDAL = new clsHost(); vw_Host vw_host = await HostDAL.GetByIdAsync(id); string actionStatus = HostDAL.Delete(vw_host); TempData["msgHost"] = actionStatus; return RedirectToAction("Display"); } 

Delete method:

public string Delete(vw_Host host) { ObjectParameter executionStatus = new ObjectParameter("ExecutionStatus", ""); try { using (Entities context = new Entities()) { string name = HttpContext.Current.User.Identity.Name.ToString(); context.sp_Host_Delete(host.ID, HttpContext.Current.User.Identity.Name.ToString(), executionStatus); context.SaveChanges(); } } catch (Exception ex) { using (Entities context = new Entities()) { context.sp_LogError(this.GetType().Name.ToString() + "." + System.Reflection.MethodBase.GetCurrentMethod().Name.ToString(), ex.Message, HttpContext.Current.User.Identity.Name); context.SaveChanges(); } executionStatus.Value = "Error occured. Please contact to Administrator"; } return executionStatus.Value.ToString(); } 

Mi problem is that when I use Async DeleteConfirmed action, in Delete method I got error:

Object reference not set to an instance of an object.

for

HttpContext.Current.User.Identity.Name 

On the other hand when I use sync action:

[HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public ActionResult DeleteConfirmed(int id) { clsHost HostDAL = new clsHost(); vw_Host vw_host = HostDAL.GetById(id); string actionStatus = HostDAL.Delete(vw_host); TempData["msgHost"] = actionStatus; return RedirectToAction("Display"); } 

Everything is working fine and HttpContext.Current.User.Identity.Name returns no error. This problem occurs only for Delete action. It works fine for Edit action (even its async).

5
  • 1
    First, make sure HttpContext.User is indeed not null. Then see stackoverflow.com/questions/28427675/… Commented Oct 14, 2016 at 10:30
  • Is sp_Host_Delete a stored-procedure? sp's does not support async. Commented Oct 14, 2016 at 10:30
  • You should also consider grabbing the user before your async calls and pass it as an argument to the delete method Commented Oct 14, 2016 at 10:32
  • @haim770 - it is null and I am wonder why it happens for async action. Commented Oct 14, 2016 at 10:37
  • @Salar - its stored procedure but even I set a variable BEFORE store procedure is reached it returns null. I have exact action for Edit method and it works fine - even it is async. Commented Oct 14, 2016 at 10:39

1 Answer 1

1

You could also consider grabbing the user before your async calls and pass it as an argument to the delete method.

[HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public async Task<ActionResult> DeleteConfirmed(int id) { var name = "Unknown"; try { name = HttpContext.Current.User.Identity.Name.ToString(); }catch { } clsHost HostDAL = new clsHost(); vw_Host vw_host = await HostDAL.GetByIdAsync(id); string actionStatus = HostDAL.Delete(vw_host, name); TempData["msgHost"] = actionStatus; return RedirectToAction("Display"); } 

Refactored Delete method

public string Delete(vw_Host host, string name) { ObjectParameter executionStatus = new ObjectParameter("ExecutionStatus", ""); try { using (Entities context = new Entities()) { context.sp_Host_Delete(host.ID, name, executionStatus); context.SaveChanges(); } } catch (Exception ex) { using (Entities context = new Entities()) { context.sp_LogError(this.GetType().Name.ToString() + "." + System.Reflection.MethodBase.GetCurrentMethod().Name.ToString(), ex.Message, name); context.SaveChanges(); } executionStatus.Value = "Error occured. Please contact to Administrator"; } return executionStatus.Value.ToString(); } 
Sign up to request clarification or add additional context in comments.

5 Comments

Link provided by @haim770 in comments does give some details for behavior stackoverflow.com/questions/28427675/…
I checked and when I move HttpContext.Current.User.Identity.Name.ToString() to Controller action, it is still null.
I had to add string actionedBy = System.Web.HttpContext.Current.User.Identity.Name; at the first item of DeleteConfirmed action and it worked. Wen I put it after vw_Host vw_host = await HostDAL.GetByIdAsync(id); action it returend error.
But that is what was advised in the answer.
I wasn't aware if that matters.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.