ex = {由于代码已优化或本机框架位于调用堆栈的顶部,因此无法评估表达式.} [英] ex = {Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}

查看:97
本文介绍了ex = {由于代码已优化或本机框架位于调用堆栈的顶部,因此无法评估表达式.}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我有两个项目,在这些项目中,我使用了从datatable到excel的导出数据.但是在一个项目中它运行良好,但在其他项目中却显示了例外:-

ex = {由于代码已优化或本机框架位于调用堆栈的顶部,因此无法评估表达式.}


我的代码是:-

私有无效Export_To_Excel(数据表dtTest,字符串strFileName,字符串strFileHeader,字符串strFromDate,字符串strToDate,int AbandonmentRate)
   {
       尝试
       {
           如果(dtTest.Rows.Count> 0)
           {

               字符串附件="attachment; filename =" + strFileName +"_" + DateTime.Now.ToString("dd_MM_yyyy_hh_mm_tt")+".xls";
               Response.ClearContent();
               Response.AddHeader("content-disposition",附件);
               Response.ContentType ="application/ms-excel";
               字符串strLine ="------------------------------";
               字符串strLineCm =";
               //-------------------
               //int cntDtRows = dtTest.Columns.Count-1;
               int cntDtRows = dtTest.Columns.Count;
               //cntDtRows--;
               String [] arrHeader =新的String [20];
               for(int p = 0; p <     cntDtRows;     p ++) 
 
                            { 
 
                                        =   dtTest.Columns [p] .ColumnName;
 
                   span>                      =   strLineCm   +   strLine;  
 
                     span>             }  
 
 
 
                   span>             //----------------------  
 
 
 
                   span>              Response.Write(< table  宽度  ="     align   =' 左'    =' > ");
               Response.Write(" tr  <   td       colspan   ='   align   =  中心 >  <   h2  >  <   u  > "+ strFileHeader +" <  /u  >  /h2  <  >  <  /tr  > );
               Response.Write(" tr  <   td       colspan   ='   align   =  居中 > (从日期" + strFromDate +到日期" + strToDate +)/td  <  /tr  > ");
               //Response.Write("<   tr  >  <   td     align   =' 右'      colspan   = ' "+ cntDtRows +"' >  Abandonment Rate("+ AbandonmentRate.ToString( )+%)/td  <  /tr  > );
               Response.Write(" tr  <   td       colspan   ='  <  /td  >  <  /tr  > );
               Response.Write(" tr  <     cntDtRows;     i ++) 
 
                            { 
 
                   span>                     valign   top'     样式  ='  background-color:green; color:white;'   宽度  ='   150'      align   =   <   >  <  字体   大小  ='  2'   面部  =' > " + arrHeader [i] .ToString()+"/font  >  <  /b  >  <  /td  > );
               }
               Response.Write("/tr   tr  <   td       colspan   ='  <  /td  >  <  /tr  > );
               foreach(dtTest.Rows中的DataRow dRow)
               {

                   Response.Write(" tr   td     valign   ='    valign   =' 顶部'   宽度  ='  150'      align   =   left  > '"+ obj.ToString()+" <  /td  > );
                           }
                           别的
                           {
                               Response.Write(" td     valign   ='    valign   =' 顶部'   宽度  ='  150'      align   =   left  > "+ obj.ToString()+" <  /td  > );
                           }
                       }
                       别的
                       {
                           Response.Write(" td     valign   ='    valign   =' 顶部'   宽度  ='  150'      align   =   left  > "+ obj.ToString()+" <  /td  > );
                       }
                       //Response.Write(" td   valign   ='  top'   宽度  ='    align   =   > " + obj.ToString()+"/td  /tr  /table  解决方案

上面共享的代码行不是触发它的确切行.它必须是Response.End行.
代替Response.End,您必须使用HttpContext.Current.ApplicationInstance.CompleteRequest方法将代码执行绕过到Application_EndRequest事件.

在此处查看详细信息: Microsoft支持:如果您使用Response.End,Response.Redirect或Server.Transfer,则会发生ThreadAbortException [ ^ ]

Hello

I have two project where I have used export data from datatable to excel. But in one project it is working fine but in other it shows the exception:-

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


My Code Is:-

private void Export_To_Excel(DataTable dtTest, String strFileName, String strFileHeader, String strFromDate, String strToDate, int AbandonmentRate)
   {
       try
       {
           if (dtTest.Rows.Count > 0)
           {

               string attachment = "attachment; filename=" + strFileName + "_" + DateTime.Now.ToString("dd_MM_yyyy_hh_mm_tt") + ".xls";
               Response.ClearContent();
               Response.AddHeader("content-disposition", attachment);
               Response.ContentType = "application/ms-excel";
               String strLine = "------------------------------";
               String strLineCm = "";
               //-------------------
               //int cntDtRows = dtTest.Columns.Count - 1;
               int cntDtRows = dtTest.Columns.Count;
               //cntDtRows--;
               String[] arrHeader = new String[20];
               for (int p = 0; p < cntDtRows; p++)

               {

                   arrHeader[p] = dtTest.Columns[p].ColumnName;

                   strLineCm = strLineCm + strLine;

               }



               //----------------------



               Response.Write("<table width=" + (cntDtRows * 150) + " align='left' valign='top'>");
               Response.Write("<tr><td  colspan='" + cntDtRows + "' align=center><h2><u>" + strFileHeader + "</u></h2></td></tr>");
               Response.Write("<tr><td  colspan='" + cntDtRows + "' align=center>(From Date " + strFromDate + "  To Date " + strToDate + ")</td></tr>");
               //Response.Write("<tr><td align='right'  colspan='" + cntDtRows + "'>Abandonment Rate( " + AbandonmentRate.ToString() + "% )</td></tr>");
               Response.Write("<tr><td  colspan='" + cntDtRows + "'>" + strLineCm + "</td></tr>");
               Response.Write("<tr>");
               for (int i = 0; i < cntDtRows; i++)

               {

                   Response.Write("<td valign='top'  style='background-color:green; color:white;' width='150'  align=left><b><font size='2' face='verdana'>" + arrHeader[i].ToString() + "</font></b></td>");
               }
               Response.Write("</tr>");
               Response.Write("<tr><td  colspan='" + cntDtRows + "'>" + strLineCm + "</td></tr>");
               foreach (DataRow dRow in dtTest.Rows)
               {

                   Response.Write("<tr>");
                   int cntCol = 0;
                   foreach (object obj in dRow.ItemArray)
                   {

                       if (cntCol == cntDtRows) { break; }
                       if (cntCol == 1 || cntCol == 5 || cntCol == 6)
                       {
                           if (obj.ToString().Length > 0)
                           {
                               Response.Write("<td  valign='top' valign='top' width='150'  align=left>'" + obj.ToString() + "</td>");
                           }
                           else
                           {
                               Response.Write("<td  valign='top' valign='top' width='150'  align=left>" + obj.ToString() + "</td>");
                           }
                       }
                       else
                       {
                           Response.Write("<td  valign='top' valign='top' width='150'  align=left>" + obj.ToString() + "</td>");
                       }
                       // Response.Write("<td valign='top' width='150'  align=left>" + obj.ToString() + "</td>");
                       cntCol++;
                   }
                   Response.Write("</tr>");

               }
               Response.Write("</table>");
               Response.End();
           }

       }
       catch (Exception ex)
       {

       }
   }




In this function it is directly download the file excel and it was working fine but now it is not working.


Is there any cause behind this then refer me.


Thanks in Advance.

解决方案

Code lines shared above is not the exact ones triggering it. It must be Response.End line.
In place of Response.End you have to use HttpContext.Current.ApplicationInstance.CompleteRequest method to bypass the code execution to the Application_EndRequest event.

Look here for details: Microsoft Support: ThreadAbortException Occurs If You Use Response.End, Response.Redirect, or Server.Transfer[^]


这篇关于ex = {由于代码已优化或本机框架位于调用堆栈的顶部,因此无法评估表达式.}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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