SAP Knowledge Base Article - Public

1523063 - ExportToHttpResponse throws exception - A first chance exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll

Symptom

  • Exporting a report using the Crystal Reports .NET SDK.
  • ExportToHttpResponse(CrystalDecisions.Shared.ExportFormatType,System.Web.HttpResponse,Boolean,String)
  • ExportToHttpResponse throws an exception.
    • A first chance exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll
    • or, {Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}
    • or, "Thread was being aborted."

thread abort error.png

Environment

  • Windows OS
  • Crystal Reports 2008
  • Visual Studio 2008

Reproducing the Issue

Code to reproduce

try
{
 boReportDocument.Load(Server.MapPath(@"MyReport.rpt"));
 boReportDocument.ExportToHttpResponse(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, Response, false, String.Empty);  // this line throws exception
}
catch (Exception ex)
{
 string s = ex.Message;
}

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.

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(CrystalDecisions.Shared.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;
}

Keywords

ADAPT00765364, 00765364, 765364 , KBA , BI-DEV-NET , BI Software Development Kits (SDKs) - .NET or Other , Bug Filed

Product

Crystal Reports 2008 V0 ; Crystal Reports 2008 V1 ; SAP Crystal Reports XI R2