简体   繁体   中英

Crystal Reports failed to open connection - at ReportDocument.Load stage.

I am currently facing an issue with Crystal Reports, whereby we keep getting this error:

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

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.

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.

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.

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.

Some code snippets:

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 > 0)
      {
        for (int i = 0; i < 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 && 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();
    }

In addition to this, one of the other things that I've noticed is that the ReportDocument.HasRecords always throws an exception.

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.

after some further examination of each MyReportToRun.rpt file, I resetted the database connection strings and refreshed each report, and ran my application again. This proved to be helpful as it worked a great deal after.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM