title: Crystal Reports failed to open connection - at ReportDocument.Load stage.
body: <p>I am currently facing an issue with Crystal Reports, whereby we keep getting this error: </p>
  
  <pre><code>PDF error: Failed to open the connection. Failed to open the connection. 4250901051516FA90CB6FED9F270A2A7 11952_8824_{CD4A46B0-F17C-4675-94B3-5F200A267CCF}.rpt  InnerException: Failed to open the connection. Failed to open the connection. 4250901051516FA90CB6FED9F270A2A7 11952_8824_{CD4A46B0-F17C-4675-94B3-5F200A267CCF}.rpt
  </code></pre>
  
  <p>Essentially, we have created a Crystal Reports processor service that Windows Task Scheduler runs. This service runs the report, exports the report as a PDF, sends the PDF report as an email, and is also stored in a central location. </p>
  
  <p>This service works fine for some reports - however, the service does not work well in debug mode, and keeps giving us this error. This error has occured, even for the reports that seem to work normally. </p>
  
  <p>This exception always occurs at the end of the Export() method stage. However, I noticed that this exception would rear its ugly head at the ReportDocument.Load(thisCrystalDocument) when the ReportDocument.HasRecords property indicates an internal exception.  </p>
  
  <p>My setup is as follows: 
  VS2013 Professional, Crystal Reports 2011, Crystal Reports 2011 SP8 Update, SAP Crystal Reports runtime engine for .NET Framework 32-bit (v15), and SQL Server 2008 R2 32bit. This is not an ASP.NET project. </p>
  
  <p>Some code snippets: </p>
  
  <pre><code>public CrystalReportGenerator(string reportLocation, string crystalParameters)
      {
        this.reportDocument = new ReportDocument();
        if (!File.Exists(reportLocation))
        {
          throw new Exception(String.Format("Report at '{0}' does not exist.", reportLocation));
        }
  
        // Cache report file
        var cacheFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + CrystalReportGenerator.cacheSubfolder);
        if (!Directory.Exists(cacheFolder))
        {
          Directory.CreateDirectory(cacheFolder);
        }
        var cachedLocation = Path.Combine(cacheFolder, reportLocation.GetMd5Sum()) + ".rpt";
        if (!File.Exists(cachedLocation))
        {
          File.Copy(reportLocation, cachedLocation, false);
        }
  
        // Generate report
        this.reportDocument.Load(cachedLocation);
        this.SetParameters(crystalParameters);
  
        // Override existing database login for report
        this.reportDocument.SetDatabaseLogon(CrystalReportGenerator.sqlUser, CrystalReportGenerator.sqlPassword, CrystalReportGenerator.sqlHost, CrystalReportGenerator.sqlDatabase, false);
  
        // Override existing database login for subreport(s)
        if (this.reportDocument.Subreports.Count &gt; 0)
        {
          for (int i = 0; i &lt; this.reportDocument.Subreports.Count; i++)
          {
            this.reportDocument.Subreports[i].SetDatabaseLogon(CrystalReportGenerator.sqlUser, CrystalReportGenerator.sqlPassword, CrystalReportGenerator.sqlHost, CrystalReportGenerator.sqlDatabase, false);
          }
        }
      }
  
  private static void GenerateReport(string reportLocation, string reportTitle, string reportParameters, string outputLocation = null, string outputFormat = null, MailMessage message = null)
      {
        var isTempFile = false;
  
        // Generate report
        using (var generator = new CrystalReportGenerator(reportLocation, reportParameters))
        {
          if (String.IsNullOrWhiteSpace(outputLocation))
          {
            outputLocation = Path.GetTempFileName();
            isTempFile = true;
          }
          else
            outputLocation = outputLocation.ConvertDateTimeReference();
  
          generator.Export(outputLocation, outputFormat);
  
          // Send mail message
          if (message != null)
          {
            using (var attachment = new Attachment(outputLocation)
            {
              Name = String.Format("{0}.{1}", reportTitle.ConvertDateTimeReference(), outputFormat.Replace("-DATA", "").ToLower())
            })
            {
              message.Attachments.Add(attachment);
              Emailer.SendMailMessage(message);
            }
          }
  
          // Clean up temp file
          if (isTempFile &amp;&amp; File.Exists(outputLocation))
          {
            File.Delete(outputLocation);
          }
        }
      }
  
  private void Export(string outputLocation, string outputFormat)
      {
        ExportOptions exportOptions = this.reportDocument.ExportOptions;
        exportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
        exportOptions.DestinationOptions = new DiskFileDestinationOptions
        {
          DiskFileName = outputLocation
        };
  
        switch (outputFormat.ToUpper())
        {
          case "XLS-DATA":
            {
              exportOptions.ExportFormatType = ExportFormatType.ExcelRecord;
              exportOptions.FormatOptions = new ExcelFormatOptions();
              break;
            }
          case "XLS":
          case "XLSX":
            {
              exportOptions.ExportFormatType = ExportFormatType.ExcelWorkbook;
              exportOptions.FormatOptions = new ExcelFormatOptions();
              break;
            }
          case "PDF":
          default:
            {
              exportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
              exportOptions.FormatOptions = new PdfRtfWordFormatOptions();
              break;
            }
        }
        this.reportDocument.Export();
      }
  </code></pre>
  
  <p>In addition to this, one of the other things that I've noticed is that the ReportDocument.HasRecords always throws an exception. </p>
  
  <p>I apologise in advance if this problem sounds a little stupid. This is my first Crystal Reports attempt and I greatly appreciate the wisdom of those more experienced than me in this area. Thank you. </p>