Thursday, April 15, 2010

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




Please visit my new Web Site WWW.Codedisplay.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:

Want to say something?
I WOULD BE DELIGHTED TO HEAR FROM YOU

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Asp.net
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
     
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash