Unable to evaluate expression… on web page

2019-03-06 06:12发布

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

I am currently seeing this in my exception:

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

Here's the offending code. The exception is thrown on response.End();

DataSet dataSet = new DataSet();
dataSet.Tables.Add(table); 
// Table is a well-formatted DataTable formed from data stored in a Session variable

HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.Charset = "";


response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", "attachment;filename=\"ExcelFile.xls\"");


using (StringWriter stringWriter = new StringWriter())
using (HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter))
{
    DataGrid dataGrid = new DataGrid { DataSource = dataSet.Tables[0] };

    dataGrid.DataBind();
    dataGrid.RenderControl(htmlTextWriter);

    response.Write(stringWriter.ToString());
    response.End();
}

This code is being used in an "export to excel" button on a web page. This is copied directly from another page that uses the same functionality that works correctly.

Any ideas on how to debug this issue? How can I get to a point where I can see the exception? Also, how does the related question apply here? The top answer and selected answers are incredibly vague.

Please note that the data in table is stored in Session state.

Thanks in advance.

3条回答
再贱就再见
2楼-- · 2019-03-06 07:03

It can be solved without getting an exception.

Instead of the

Response.End()

use the

Response.Flush()

function which does not stop the execution of the page.

I've also had some problems when I've tried to download an .xlsx file using the Reponse of the page: after opening the excel file I've got the following error text:

Excel found unreadble content in myFilename.xlsx. Do you want to recover the contents of this workbook? If you trust the source of this book, click Yes.

Solved this with adding Response.SuppressContent = true; after the Response.Flush().

查看更多
We Are One
3楼-- · 2019-03-06 07:12

Use HttpContext.Current.ApplicationInstance.CompleteRequest() instead of Response.End()

查看更多
时光不老,我们不散
4楼-- · 2019-03-06 07:17

The code is fine. When you do Response.End() you get a ThreadAbortException:

http://support.microsoft.com/kb/312629/EN-US/

Some people consider Response.End() too drastic though:

Is Response.End() considered harmful?

I would suggest handling this specific exception (since you know you are going to get it), and moving the reponse.End() (like mellamokb suggested):

        HttpResponse response = HttpContext.Current.Response;
        try
        {
            DataSet dataSet = new DataSet();
            DataTable table = new DataTable();

            dataSet.Tables.Add(table);

            response.Clear();
            response.Charset = "";

            response.ContentType = "application/vnd.ms-excel";
            response.AddHeader("Content-Disposition", "attachment;filename=\"ExcelFile.xls\"");

            using (StringWriter stringWriter = new StringWriter())
            using (HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter))
            {
                DataGrid dataGrid = new DataGrid { DataSource = dataSet.Tables[0] };

                dataGrid.DataBind();
                dataGrid.RenderControl(htmlTextWriter);

                response.Write(stringWriter.ToString());

            }
            response.End();
        }
        catch (ThreadAbortException ex)
        {
            //Log some trace info here
        }
查看更多
登录 后发表回答