I am creating a viewer that will set login information and allow the users to view, print or export reports. VB.NET 2010 WinForms Crystal Engine 13.0.9.1312. All reports are external designed by various programmers.
The preview mode of my viewer is now working quite well (with the help of Don and Ludek most recently). Anything I want to see on screen works great in the regular Crystal viewer.
The problem is when I want to export to pdf or print to a printer without viewing on screen in the regular Crystal viewer.
If there are no parameters in the reports, both exporting to pdf and printing to any printer selected works.
When there is one or more parameters, it crashes and points to the line
crReport.ExportToDisk(ExportFormatType.PortableDocFormat, strFile)
or
crReport.PrintToPrinter(1, True, 0, 0)
with the message Missing Parameter Values.
Through some searching (and yes I have checked on this site and Google for "Missing Parameter Values") I have found one big issue is that parameter values have to be set after the .ReportSource is set. So before I changed my code, I never saw the Crystal Parameter prompt screen. Now I see it but still get the crash (though once I have had it go to printer honouring the Parameter in a report, but I can't always reproduce it and never when sending to pdf).
The reports may be designed by end users who are using my program. I will not know what reports they are or if they have even put in parameters. I will not know if they are date, string, number, discrete, range, multiple, etc.
The original login code referred to below can be found here.
'original login code here down to subreport login
'rest of subroutine is just a Catch and End Try
crViewer.ReportSource = Nothing
crViewer.ReportSource = crReport
'check if to be sent direct to printer and send all pages
If Not autoforms Is Nothing AndAlso autoforms.ToPrinter Then
crReport.PrintOptions.PrinterName = autoforms.Printer
crReport.PrintToPrinter(1, True, 0, 0) '-> Error Triggers HereEnd If
'setup viewer options
Dim intExportFormatFlags As Integer
intExportFormatFlags = ViewerExportFormats.PdfFormat Or
ViewerExportFormats.ExcelFormat Or
ViewerExportFormats.ExcelRecordFormat Or
ViewerExportFormats.XLSXFormat Or
ViewerExportFormats.CsvFormat
crViewer.AllowedExportFormats = intExportFormatFlags
crViewer.ShowLogo = False
crViewer.ToolPanelView = ToolPanelViewType.GroupTree
'check if to be sent direct to pdf and send all pages
If Not autoforms Is Nothing AndAlso autoforms.ToPDF ThenDim strPDFFilename As String = autoforms.PDFPath & "\" & autoforms.ReportName & "_" & autoforms.ReportType & ".pdf"
'strPDFFilename = Replace(strPDFFilename, " ", "_")SendExport(strPDFFilename, "PDF")
End If
'if not preview then close form
If Not autoforms Is Nothing AndAlso Not autoforms.Preview Then
Me.Close()
Exit Sub
End If
Private Sub SendExport(ByVal strFile As String, ByVal strExportFormat As String)
'pass current parameter values to the pdf
Dim crParamFieldDefs As ParameterFieldDefinitions
Dim crParamFieldDef As ParameterFieldDefinition
Dim crParamValues As New ParameterValuesDim x As Integer = crViewer.ParameterFieldInfo.Count
crParamFieldDefs = crReport.DataDefinition.ParameterFields'loop through each parameter and assign it the current values
For Each crParamFieldDef In crParamFieldDefs
crParamValues = crParamFieldDef.CurrentValuescrParamFieldDef.ApplyCurrentValues(crParamValues)
Next
Select Case strExportFormat
Case Is = "PDF"
crReport.ExportToDisk(ExportFormatType.PortableDocFormat, strFile) '--> Error Triggers Here
Case Is = "xls"
crReport.ExportToDisk(ExportFormatType.Excel, strFile)
Case Is = "xlsx"
crReport.ExportToDisk(ExportFormatType.ExcelWorkbook, strFile)
End Select
End Sub
This code doesn't work on my system, but my logic is as follows (as flawed as it is). Look through the parameters, find the current values and then assign them to the parameters again just before export.
With the code above, I am getting the enter parameter values screen in the viewer. Therefore, my assumption is that the report now has a current value(s) for the parameter(s). The loop will not require the program to know how may parameters there are or what the values or value types are.
There are a couple of problems with this. When I step through it, there are no current values, because the prompt doesn't come up with stepping through the code with F8. Second problem is that it is finding the internal parameters used for subreport linking ({?PM-xxxxx}). Third problem is that a lot of code I have looked at does not appear like it is assigning the reportsource to a viewer at all just to export or print to printer. So is it necessary to do as I have done here?
I am sorry this is so long and I appreciate anyone who has stuck to it this long. I am at my wits end trying to figure out how to make this work. I think there is a timing issue here but do not know how to resolve it. I just want the report to trigger the prompts if there are parameters, then send the report screen, to pdf or to printer depending on the user's settings for the report (it may be any or all three options). If you need more info, let me know.
TIA, rasinc