Thread was being aborted exception in crystal reports
Asked Answered
N

3

5

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.

Niel answered 23/1, 2012 at 7:2 Comment(0)
U
6

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;
}
Umbilicus answered 13/2, 2012 at 12:59 Comment(1)
For adding a name to the file you can use Response.AddHeader("content-disposition", "attachment; filename=Report " + DateTime.Now.ToString() + ".pdf"); instead of Response.ClearHeaders();Edmea
T
0

The error is thrown because a call to response.End() is made inside of ExportToHttpResponse. Remove your calls to Flush and Close the response and wrap your call to ExportToHttpResponse inside a try/catch block to catch and ignore the System.Threading.ThreadAbortException.

Trout answered 23/1, 2012 at 15:56 Comment(0)
R
0

The error happens due to the response.End() that is made inside of ExportToHttpResponse. Since your code maybe calling ExportToHttpResponse and then doing some other stuff, then the thread abort exception is raised. So avoid any line of code after ExportToHttpResponse is called. Even with try-catch wrapping the abort exception is raised.

So make sure your last line in your event method contains ExportToHttpResponse.

go from

private void some_Command(object source, CommandEventArgs e)
        {
          try
          {
            //some other code her
    repDoc.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, "Some title ");
          }
          Catch
          {
          }
        }

to

    private void some_Command(object source, CommandEventArgs e)
    {
      try
      {
        //some other code here
      }
      catch
      {
      }
      repDoc.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, "Some title ");
    }
Reek answered 13/9, 2023 at 11:8 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.