Click here to Skip to main content
15,910,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i'm having a list of data into "Custlist" but whenever i want to save it into a excel file, got an error "unable to evaluate expression because the code is optimized or a native frame is on top of the call stack".. is there anything to write in web.config file????










List<clscustexcel> Custlist = new List<clscustexcel>();
Custlist = obj.Fetch_CustomerManagementForExcel();
if (Custlist.Count > 0)
{
string filename = "Customer.xls";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid dgGrid = new DataGrid();
dgGrid.DataSource = Custlist;
dgGrid.DataBind();

//Get the HTML for the control.
dgGrid.RenderControl(hw);
//Write the HTML back to the browser.

Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
this.EnableViewState = false;
Response.Write(tw.ToString());
Response.End();
}
Posted

An error like this occurs most of the time when you are trying to redirect to a new page, while the current page is still trying to complete it's process. You can find more information about the issue right here:
http://support.microsoft.com/kb/312629/EN-US/
Your call of
C#
Response.End()
possibly causes a threadAbortionException.
Adding few lines like these may help:
C#
HttpContext.Current.ApplicationInstance.CompleteRequest();
Response.Redirect ("myexamplepage.aspx", false);
 
Share this answer
 
Comments
prthghosh999 15-Sep-13 6:55am    
i've written
HttpContext.Current..
instead of Response.End() and exception is not thrown..
but i'm not getting the excel file also
prthghosh999 15-Sep-13 6:55am    
i've written
HttpContext.Current..
instead of Response.End() and exception is not thrown..
but i'm not getting the excel file also
MCY 15-Sep-13 15:23pm    
I see. maybe debugging and tracing line by line helps to find why no excel is created?
some years ago i did it with datatable,hope to help you
C#
public void ToExcel(DataTable dt, string reportName)
    {
        if (dt.Rows.Count > 0)
        {
            string filename = string.Format("{0}.xls", reportName);
            System.IO.StringWriter sw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
            HtmlForm hf = new HtmlForm();

            DataGrid dg = new DataGrid();

            dg.DataSource = dt;
            dg.HeaderStyle.BackColor = Color.Tomato;
            dg.HeaderStyle.ForeColor = Color.White;
            dg.BackColor = Color.LightGoldenrodYellow;
            dg.AlternatingItemStyle.BackColor = Color.PaleGoldenrod;
            dg.Font.Name = "Tahoma";
            dg.Font.Size = 10;
            dg.DataBind();

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");

            HttpContext.Current.Response.Charset = "";
            EnableViewState = false;
            Controls.Add(hf);
            hf.Controls.Add(dg);
            hf.RenderControl(htw);
            HttpContext.Current.Response.Write(sw.ToString());
            HttpContext.Current.Response.End();
        }
    }
 
Share this answer
 

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