Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have a button that Exports to Excel. It works fine and all until it get's to the Response.End();
After that I get an error saying {Text = Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.} After that the a window pops up that asks if you want to save or view the file. It shows that it's a Excel file, but as soon as I view it, it opens in Visual Studio 2009. Any help on how to solve this problem would be appreciated, here is the code I am currently using:

C#
public override void VerifyRenderingInServerForm(Control control)
    {
        //Verifies that the control is rendered
    }

    protected void btnExportToExcel_Click(object sender, EventArgs e)
    {
        try
        {
            if (gvCustomData.Rows.Count > 0)
            {
                Response.Clear();
                Response.Buffer = true;
                Response.ContentType = "application/vnd.ms-excel";
                // Response.ContentType ="application/text";
                Response.AddHeader("Content-Disposition", "attachment;CustomData.xls");

                Response.Charset = "";
                this.EnableViewState = false;

                System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
                System.Web.UI.Html32TextWriter oHtmlTextWriter = new System.Web.UI.Html32TextWriter(oStringWriter);
                gvCustomData.RenderControl(oHtmlTextWriter);

                Response.Write(oStringWriter.ToString());
                Response.End();
            }
        }

        catch (Exception ex)
        {
            Adhoc.msg(this, "Error: " + ex.Message);
        }
    }
Posted
Updated 19-Sep-13 22:55pm
v2

Just Remove the Try Catch Block it will work.
 
Share this answer
 
You may find some answers in this link.

http://support.microsoft.com/kb/312629/en-us[^]
 
Share this answer
 
Comments
[no name] 20-Sep-13 5:30am    
The HttpContext.Current.ApplicationInstance.CompleteRequest works, but it's still opening in Visual Studio 2009 instead of Excel?
Thomas ktg 20-Sep-13 5:37am    
I am not pretty sure about the solution for the problem you get while opening. Make sure the excel you have installed supports for the Visual Studios version.
[no name] 20-Sep-13 5:40am    
Thanks, Solved the problem, missing reference
Member 10587608 19-Aug-14 1:33am    
hi pierre3557..same error i m getting but excel is not generating and on response.end() giving same error..
Member 10587608 19-Aug-14 1:33am    
can u please tell what you did for the same..??

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