Click here to Skip to main content
16,012,759 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:-
XML
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.
Posted

1 solution

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[^]
 
Share this answer
 

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