Please visit my new Web Site https://coderstechzone.com
Error:
Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack
Reason/Root Cause:
When we call Response.Redirect("") to redirect an aspx page to another aspx page from try/catch block in asp.net code, you will receive the above error.
Sample error code:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { try { // Your other code or logic here Response.Redirect("Your Redirect URL"); } catch (Exception ex) { Response.Redirect("Your Custom Error Page URL"); } } }
Solution:
Keep in mind that when you use Response.Redirect or Server.Transfer within try block then use the overload method of Response.Redirect() in the below way will resolve your problem.
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { try { // Your other code or logic here Response.Redirect("Your Redirect URL", false); } catch (Exception ex) { Response.Redirect("Your Custom Error Page URL"); } } }
By setting the second bollean argument of Response.Redirect to false will resolve your problem. On the other hand you can send the redirection code outside of try block.
For more details regarding this error:
ThreadAbortException Occurs If You Use Response.End, Response.Redirect, or Server.Transfer
0 comments:
I WOULD BE DELIGHTED TO HEAR FROM YOU