C# – Unable to evaluate expression because the code is optimized or a native frame is on top of call stack

asp.netc

I am working on a website where user comes and submits some information. After the information is submitted I am trying to send two mails simultaneously – one to my sales team and one to the visitor on my website.

While the mails are getting sent I am getting the following error while redirecting to another page using Response.Redirect("http://www.targetsite.com/index.php"):

Unable to evaluate expression because the code is optimized or a
native frame is on top of call stack

Can anybody help me with this issue? Thanks in advance.

Best Answer

That message you're seeing really isn't an error you see at runtime - typically, its seen in the debugger in the middle of the catch portion of a try-catch-finally block. Are you stepping through the code in the debugger, or are you actually getting the error as the application runs outside the debugger?

If you're trying to determine what error is really occurring, I'd recommend putting a full try-catch block around the Response.Redirect call and inspect the exception that fires at that point, eg

Try
   Response.Redirect(http://www.targetsite.com/index.php")
Catch ex as Exception 
   ' add your handling code here, using ex as the Exception placeholder variable
End Try

Good luck!