Click here to Skip to main content
14,691,895 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am using following code for exporting data table record in excel.It does not work for me.I am getting exception "Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack".

How I resolve my problem. Please Help me

protected void ExportDatatableToXLS(DataTable dtExports)
    {
        Response.Clear();
        Response.ClearContent();
        Response.ClearHeaders();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=Member.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";

        Table mainTable = new Table();

        TableRow trExport;
        TableCell cellExport;

        if (dtExports.Rows.Count > 0)
        {
            trExport = new TableRow();
            for (int col = 0; col < dtExports.Columns.Count; col++)
            {
                cellExport = new TableCell();
                cellExport.Text = dtExports.Columns[col].ToString();
                cellExport.BorderWidth = 1;
                cellExport.Style.Add("font-weight", "bold");
                cellExport.Style.Add("text-align", "Left");
                cellExport.BackColor = System.Drawing.Color.Silver;
                trExport.Cells.Add(cellExport);
                mainTable.Rows.Add(trExport);
            }

            for (Int32 ai = 0; ai < dtExports.Rows.Count; ai++)
            {
                trExport = new TableRow();
                for (Int16 cl = 0; cl < dtExports.Columns.Count; cl++)
                {
                    cellExport = new TableCell();
                    cellExport.Text = Convert.ToString(dtExports.Rows[ai][cl]).Trim();
                    cellExport.BorderWidth = 1;
                    cellExport.Style.Add("text-align", "left");
                    cellExport.Style.Add("vertical-align", "top");
                    trExport.Cells.Add(cellExport);
                }
                mainTable.Rows.Add(trExport);
            }
        }

        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        mainTable.RenderControl(hw);
        Response.Flush();
        Response.Write(hw.InnerWriter.ToString());
        Response.Flush();
        Response.End();
    }
Posted
Updated 22-May-12 7:23am
v3

1 solution

You just put this code before of the final catch statement
catch (System.Threading.ThreadAbortException lException)
{
// do nothing
}


Another silly cause for this exception, when you are working file download window in asp.net. The reason is when the downloaded file name contains any spaces then u may get this error. You can keep an eye on that.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900