gpt4 book ai didi

c# - 无法评估表情…在网页上

转载 作者:行者123 更新时间:2023-12-03 08:03:44 27 4
gpt4 key购买 nike

与此问题相关:Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack

我目前在我的异常中看到了这一点:

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



这是令人反感的代码。在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();
}

网页上的“导出到Excel”按钮中使用了此代码。这是直接从另一个页面复制而来的,该页面使用的功能正常。

关于如何调试此问题的任何想法?我怎样才能看到异常?另外,相关问题在这里如何适用?最佳答案和所选答案非常模糊。

请注意, table中的数据以 session 状态存储。

提前致谢。

最佳答案

代码很好。当您执行Response.End()时,您将获得ThreadAbortException:

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

有些人认为Response.End()太过激:

Is Response.End() considered harmful?

我建议处理此特定异常(因为您知道将要得到它),并移动reponse.End()(如建议的mellamokb):

        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
}

关于c# - 无法评估表情…在网页上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10756359/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com