1

Getting an error while exporting the selected rows of grid into excel. error: Unable to evaluate expression because the code is optimized or a native frame is on top of the stack.

Actually I want to export the selected rows from the grid to excel, so the user can easily download the selected records please refer the code for Export to excel Onclick method.

protected void btnExportExcel_Click(object sender, EventArgs e)
    {
                bool isSelected = false;
                foreach (GridViewRow i in gvDistrictSchoolReport.Rows)
                {
                    CheckBox cb = (CheckBox)i.FindControl("Chkbox");
                    if (cb != null && cb.Checked)
                    {
                        isSelected = true;
                        break;
                    }
                }
                if (isSelected)
                {
                    GridView gvExport = gvDistrictSchoolReport;
                    // this below line for not export checkbox to excel file
                    gvExport.Columns[0].Visible = false;
                    foreach (GridViewRow i in gvDistrictSchoolReport.Rows)
                    {
                        gvExport.Rows[i.RowIndex].Visible = false;
                        CheckBox cb = (CheckBox)i.FindControl("Chkbox");
                        if (cb != null && cb.Checked)
                        {
                            gvExport.Rows[i.RowIndex].Visible = true;
                        }
                    }
                    try
                    {
                        Response.Clear();
                        Response.Buffer = true;
                        Response.AddHeader("content-disposition", "attachment;filename=ExportGridData.xls");
                        Response.Charset = "";
                        Response.ContentType = "application/vnd.ms-excel";
                        StringWriter sw = new StringWriter();
                        HtmlTextWriter htW = new HtmlTextWriter(sw);
                        gvExport.RenderControl(htW);
                        Response.Output.Write(sw.ToString());
                        Response.Flush();
                        Response.End();

                    }
                    catch(Exception ex)
                    {
                        Console.WriteLine(ex);
                    }

                }
            }
K Abhishek
  • 99
  • 7
  • There're some questions and answers dealing with this exception. Some examples: https://stackoverflow.com/questions/2041482/unable-to-evaluate-expression-because-the-code-is-optimized-or-a-native-frame-is, https://stackoverflow.com/questions/10756359/unable-to-evaluate-expression-on-web-page. You should also take a look here: [MS KB for ThreadAbortException](https://support.microsoft.com/en-us/help/312629/prb-threadabortexception-occurs-if-you-use-response-end-response-redir) – eyal May 22 '19 at 08:01

1 Answers1

3

If you are using update panel then use PostBack Triggers like this

<asp:UpdatePanel ID="up1" runat="server">
        <Triggers>
        <asp:PostBackTrigger ControlID="gvDistrictSchoolReport" /> -- for grid
             <asp:PostBackTrigger ControlID="Button6" />  -- for export to excel button
    </Triggers>

before the grid. Hope this will work

Abhishek Gupta
  • 230
  • 2
  • 12