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

Today, i will discuss about Response.End, Response.Redirect, and Server.Transfer methods which returns error sometime like “Thread was being aborted” or “Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.” etc….and why they return it??

Here is reason for error :-

The Response.End method ends the page execution and shifts the execution to the Application_EndRequest event in the application’s event pipeline. The line of code that follows Response.End is not executed.

This problem occurs in the Response.Redirect and Server.Transfer methods because both methods call Response.End internally.

Solution For This Errors :-

For Response.End, call the HttpContext.Current.ApplicationInstance.CompleteRequest method instead of Response.End to bypass the code execution to the Application_EndRequest event.

For Response.Redirect, use an overload, Response.Redirect(String url, bool endResponse) that passes false for the endResponse parameter to suppress the internal call to Response.End.
For example:Response.Redirect (“nextpage.aspx”, false);

If you use this workaround, the code that follows Response.Redirect is executed.
For Server.Transfer, use the Server.Execute method instead.

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>