Our company currently has a WCF service that prints our crystal reports. It works 99% of the time. However, I am having trouble with a couple of our reports. Namely, when trying to export them to a PDF I get one o fthe following errors depending on the order things have been done in:
1. "The system cannot find the path specified."
2. "Missing parameter values."
3. The report prints, but shows data from the wrong server.
The Reports are fairly simple reports that are attached to SQL Server stored procedures that take (at most) 2 or 3 parameters. They were designed in Crystal Reports 2011, and setting the datasource location to any of our servers allows them to print fine. Also, just re-opening the report and logging on to a different server works, so long as it's done through Crystal Reports.
I have made sure that the stored procedures are simple enough, and that the parameters are of simple types. I have also tried recreating the report files from scratch and pointing them at a new stored procedure. I have ensured that data is not being saved with the reports. I've tried removing sections of the report until thye work (they only do once everything is removed).
The outline of our code is as follows (I have commented several sections that may help someone track down why certain errors are thrown):
Public Sub PrintReport()
Dim resultStream As Stream = Stream.Null
Dim reportDoc As New ReportDocument
Dim saved As Boolean = False
Dim exportOptions As ExportOptions
Dim diskFileDestinationOptions As DiskFileDestinationOptions
Dim connectionInfo As ConnectionInfo
reportDoc.PrintOptions.DissociatePageSizeAndPrinterPaperSize = False
reportDoc.PrintOptions.PrinterName = ""
'Retrieves the SQL Server credentials from an excrypted location, including
'sets them like this:
'With Credentials
' ci.DatabaseName = .DBName
' ci.ServerName = .Server
' ci.UserID = oCrypt.NICO_RijndaelManaged_Decrypt(.Login)
' ci.Password = oCrypt.NICO_RijndaelManaged_Decrypt(.Password)
' schema = .Schema
'End With
connectionInfo = SetAppConnection()
diskFileDestinationOptions = New DiskFileDestinationOptions
reportDoc.FileName = settings.ReportDir & settings.ConnectionStrings(applicationID).Application & report.FormFilePath
reportDoc.Load(reportDoc.FileName, OpenReportMethod.OpenReportByTempCopy)
'Loops over Report Parameters and assigns them from a FormParameter Class (Name and Value are both strings)
'Public Sub SetParameters(ByRef rptDoc As ReportDocument, ByVal objRpt As Report)
' For Each PField As ParameterField In rptDoc.ParameterFields
' Dim Param = PField
' If objRpt.Params.Any(Function(P) P.Name = Param.Name) Then
' Dim Value As String = (From P In objRpt.Params Where P.Name = Param.Name Select V = P.Value).First()
' rptDoc.SetParameterValue(Param.Name, Value.Trim)
' End If
' Next
'End Sub
SetParameters(reportDoc, report)
'See Below
SetDataInfo(reportDoc, connectionInfo)
exportOptions = reportDoc.ExportOptions
With exportOptions
.DestinationOptions = diskFileDestinationOptions
.ExportDestinationType = ExportDestinationType.NoDestination
.ExportFormatType = ExportFormatType.PortableDocFormat
End With
Dim intTry As Integer = 0
Dim exExport As New Exception
While Not saved And intTry < 17
Try
resultStream = reportDoc.ExportToStream(ExportFormatType.PortableDocFormat)
saved = True
Catch ex As Exception
'Allowing this piece to execute fixes the "Path Specified" issue, but throws a missing parameter values error
'even though the parameters ARE SET on the report document object when examining it with a watch.
If ex.Message = "The system cannot find the path specified." Then
reportDoc.VerifyDatabase()
End If
'Allowing this piece to execute allows the report to print, however it seems to be running with the server information
'that it was designed with instead of the information that was set above (see below). Despite this, the server information
'appears to be set correctly when examining the members of the report document.
If ex.Message.Contains("Missing") Then
SetParameters(reportDoc, report)
End If
intTry += 1
End Try
End While
reportDoc.Close()
reportDoc.Dispose()
End Sub
Public Sub SetDataInfo(ByRef rptDoc As ReportDocument, ByVal objCI As ConnectionInfo)
Dim crTables As Tables = rptDoc.Database.Tables
Dim crObjs As ReportObjects
Dim crObj As ReportObject
crObjs = rptDoc.ReportDefinition.ReportObjects
Dim crSubTables As Tables
Dim crSubTable As Table
Dim crTLOI As New TableLogOnInfo
crTLOI.ConnectionInfo = objCI
For Each crTable As Table In crTables
crTLOI.TableName = crTable.Location.Substring(crTable.Location.LastIndexOf(".") + 1)
crTable.ApplyLogOnInfo(crTLOI)
crTable.Location = crTable.Location.Substring(crTable.Location.LastIndexOf(".") + 1)
Next
For Each crObj In crObjs
If crObj.Kind = ReportObjectKind.SubreportObject Then
Dim rptSub As ReportDocument = DirectCast(crObj, SubreportObject).OpenSubreport(DirectCast(crObj, SubreportObject).SubreportName)
crSubTables = rptSub.Database.Tables
For Each crSubTable In crSubTables
crSubTable.ApplyLogOnInfo(crTLOI)
crSubTable.Location = crSubTable.Location.Substring(crSubTable.Location.LastIndexOf(".") + 1)
Next
End If
Next
End Sub
The versions of the Crystal Libraries being used are Support Pack (Or Service Pack, depending on which part of their website you're looking at) 13 from version 13--v.13.0.13.1597
I really need to get these reports to print and be able to switch the servers depending on the environment. If you have any insight, please help.