无法评估EX pression ......在网页上 [英] Unable to evaluate expression... on web page

查看:149
本文介绍了无法评估EX pression ......在网页上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与此相关的问题:<一href="http://stackoverflow.com/questions/2041482/unable-to-evaluate-ex$p$pssion-because-the-$c$c-is-optimized-or-a-native-frame-is">Unable评估前pression因为code优化或本机框架是在调用堆栈

我目前看到这在我的异常:

I am currently seeing this in my exception:

{无法评估EX pression因为code优化或本机框架调用堆栈的顶部。}

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

下面是有问题的code。唯一的例外是抛出到Response.End();

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();
}

这code被用在导出到Excel按钮,在网页上。这是直接从使用的正常工作相同的功能的另一页复印。

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.

在此先感谢。

推荐答案

在code是罚款。当你到Response.End(),你会得到一个ThreadAbortException:

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

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

有些人认为到Response.End()过于激烈,但:

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

是到Response.End()被认为有害吗?

我会建议处理这个特定的异常(因为你知道你会得到它),移动reponse.End()(如mellamokb建议):

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
        }

这篇关于无法评估EX pression ......在网页上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆