Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread was being aborted exception in crystal reports

We were getting the Thread was being aborted Exception while exporting a report into PDF.

The below code we were using for export a report into PDF.

                    Response.Buffer = true;
                    Response.ClearContent();
                    Response.ClearHeaders();
                    Response.ContentType = "application/pdf";
                    myReportDoc.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, Session["ReportName"].ToString());
                    Response.Flush();
                    Response.Close();

Please help me how to resolve this exception.

like image 260
Nagamuneendrareddy E Avatar asked Jan 23 '12 07:01

Nagamuneendrareddy E


People also ask

What causes thread abort exception?

When a call is made to the Abort method to destroy a thread, the common language runtime throws a ThreadAbortException on . NET Framework. ThreadAbortException is a special exception that can be caught, but it will automatically be raised again at the end of the catch block.

What is thread being aborted?

If the thread that calls Abort holds a lock that the aborted thread requires, a deadlock can occur. If Abort is called on a thread that has not been started, the thread will abort when Start is called. If Abort is called on a thread that is blocked or is sleeping, the thread is interrupted and then aborted.


1 Answers

SAP explains that:

Cause

The issue has been identified and logged under Problem Report ID ADAPT00765364. The error is likely caused because Response.End() is used inside the ExportToHttpResponse() method.
It is a known issue that Reponse.End() causes the thread to abort. This is by design.
See Microsoft KB312629 Article for more info.

Workaround

....
 try
   {
   reportDocument.ExportToHttpResponse(format, Response, true, Page.Title);
   }
 catch (System.Threading.ThreadAbortException)
   {
   }
....

Resolution

You can write your own code to export a Crystal Report directly to the browser in a format such as PDF, Word, Excel, etc. You must make sure you use the appropriate content type.

Sample code to export Crystal Report to web browser as PDF

try
{
 boReportDocument.Load(Server.MapPath(@"MyReport.rpt"));
 System.IO.Stream oStream = null;
 byte[] byteArray = null;
 oStream = boReportDocument.ExportToStream (ExportFormatType.PortableDocFormat);
 byteArray = new byte[oStream.Length];
 oStream.Read(byteArray, 0, Convert.ToInt32(oStream.Length - 1));
 Response.ClearContent();
 Response.ClearHeaders();
 Response.ContentType = "application/pdf";
 Response.BinaryWrite(byteArray);
 Response.Flush();
 Response.Close();
 boReportDocument.Close();
 boReportDocument.Dispose();

}
catch (Exception ex)
{
 string s = ex.Message;
}
like image 138
Emanuele Greco Avatar answered Oct 29 '22 00:10

Emanuele Greco