Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have two buttons with onclick event handlers that process information from two grid views on the page. One button for each gv. These are situated on top of each other nested in a html table structure.

The buttons are used to export the grid date into an Excel document (see code below)

The top button and grid when click on the button works fine but the bottom button throws a ThreadAbortException: Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.

Naturally I've Google-d for this but some of the top results were dealing with Response.Redirect() calls rather than Response.End(). One such post at Forums.asp.net has the same error on the same method call but the solution was to changing the code Response.Redirect() with an error page as parameter - again not related to what I have.

Another search at Microsoft Suport page suggest a solution where HttpContext.Current.ApplicationInstance.CompleteRequest() replaces Response.End(). I've tried this, the error goes away and so is the Excel download popup.

So I don't know where to go from here. What's weird is that the same code (less gridview id) works for one but the other. Here is the code for your review and I've marked where the error is thrown. I thought perhaps I could spawn an new thread - would that alleviate the issue? I've never done a multi-threaded app but I'm up for a challenge.

<table>
  <tr>
   <td align="left">
     <asp:Button ID="btnExport" runat="server" OnClick="btnExport_Click"
          Text="Export" Visible="false" />
   </td>
  </tr>
  <tr>
   <td>                    
     <asp:Panel runat="server" ID="pnl1" Visible="false">
      <asp:GridView ID="gvCountTotalsCat" runat="server" 
          AutoGenerateColumns="false"
          CellPadding="3" PageSize="25" BackColor="White" BorderColor="MidnightBlue"
          BorderStyle="Groove" BorderWidth="1px" CssClass="TextCompact" 
          GridLines="Vertical"
          OnRowDataBound="gridView_OnRowDataBound"
          EmptyDataText="Your request has returned zero records">
          <Columns>
           <asp:TemplateField>
            <HeaderTemplate>
             <asp:Label runat="server" ID="lblHeader" Text="Cat" />
            </HeaderTemplate>
           <ItemTemplate>
            <asp:Literal ID="litWuc" runat="server" />
           </ItemTemplate>
           </asp:TemplateField>
            <asp:BoundField DataField="Cat Entries" HeaderText="Cat Entries" />
           <asp:TemplateField>
           <HeaderTemplate>
            <asp:Label runat="server" ID="lblHeader" />
           </HeaderTemplate>
           <ItemTemplate>
            <asp:Literal ID="litSum" runat="server" />
           </ItemTemplate>
          </asp:TemplateField>
         </Columns>                                              
        </asp:GridView>
       </asp:Panel>
   </td>
  </tr>
  <tr>
   <td align="left">
    <asp:Button ID="btnExport1" runat="server" OnClick="btnExport_Click1" 
         Text="Export" Visible="false" />
   </td>
  </tr>
  <tr>
   <td>                         
    <asp:Panel runat="server" ID="pnl2" Visible="false">
     <asp:GridView ID="gvCountTotalsCat1" runat="server" 
          AutoGenerateColumns="false"
          AllowPaging="false" CellPadding="3" PageSize="25" BackColor="White"
          BorderColor="MidnightBlue" BorderStyle="Groove" BorderWidth="1px" 
          CssClass="TextCompact" GridLines="Vertical" 
          OnRowDataBound="gridView_OnRowDataBound"
          EmptyDataText="Your request has returned zero records">
          <Columns>
            <asp:TemplateField>
             <HeaderTemplate>
              <asp:Label runat="server" ID="lblHeaderWuc" Text="Wuc" />
             </HeaderTemplate>
            <ItemTemplate>
            <asp:Literal ID="litWuc" runat="server" />
            </ItemTemplate>
           </asp:TemplateField>
           <asp:BoundField DataField="Wuc Entries" HeaderText="Wuc Entries" />
           <asp:TemplateField>
            <HeaderTemplate>
              <asp:Label runat="server" ID="lblHeader" />
            </HeaderTemplate>
            <ItemTemplate>
              <asp:Literal ID="litSum" runat="server" />
            </ItemTemplate>
           </asp:TemplateField>
          </Columns>     
         </asp:GridView>
        </asp:Panel>
       </td>
      </tr>
     </table>



public void btnExport_Click(object sender, System.EventArgs e)
{
    string attachment = string.Empty;
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);

    // Create a form to contain the grid
    HtmlForm frm = new HtmlForm();
    frm.Attributes["runat"] = "server";

    attachment = "attachment; filename=gvCountTotalsCat_" + _selectedSite + ".xls";
    gvCountTotalsCat.Parent.Controls.Add(frm);
    frm.Controls.Add(gvCountTotalsCat);

    Response.ClearContent();
    Response.AddHeader("content-disposition", attachment);
    Response.ContentType = "application/ms-excel";

    frm.RenderControl(htw);

    Response.Write(sw.ToString());

    Response.End();
}



public void btnExport_Click1(object sender, System.EventArgs e)
{
    string attachment = string.Empty;
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    Response.Clear();
    Response.ClearHeaders();

    // Create a form to contain the grid
    HtmlForm frm = new HtmlForm();
    frm.Attributes["runat"] = "server";

    attachment = "attachment; filename=gvCountTotalsCat1_" + _selectedSite + ".xls";
    gvCountTotalsCat1.Parent.Controls.Add(frm);
    frm.Controls.Add(gvCountTotalsCat1);

    Response.ClearContent();
    Response.AddHeader("content-disposition", attachment);
    Response.ContentType = "application/ms-excel";

    frm.RenderControl(htw);

    Response.Write(sw.ToString());

    try
    {
 >> Error thrown here >>     Response.End();

    }
    catch (System.Threading.ThreadAbortException lException)
    {
        lException;
    }
}
like image 770
Risho Avatar asked Mar 14 '13 19:03

Risho


2 Answers

It runed out that I have both grids and buttons in one Update Panel and only the top button was set as a PostBackTrigger. After I added the second one <asp:PostBackTrigger ControlID="btnExport1" /> and that solve the problem.

like image 167
Risho Avatar answered Nov 19 '22 15:11

Risho


There is a handy way to deal with "Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack." issues. You need to write on the Output window.

Add using System.Diagnostics;

Add a Try/Catch for the line that is erroring

In the Catch add these lines

try
{ ..}
 catch(Exception ex)
{
    Debug.WriteLine(ex.Message);
    Debug.WriteLine(ex.StackTrace);
    Debug.WriteLine(ex.InnerException.ToString());
}

Just debug and check the Output window

Hope it helps.

like image 43
Santhosh Avatar answered Nov 19 '22 16:11

Santhosh