views:

837

answers:

3

I have a report that I created with Crystal Reports 2008. This report uses upwards of 40 parameters to control the layout of the report. All the parameters are boolean and are defaulted to False in the saved report. The report is saved with the save data with the report option unchecked as I want to read the results from the database rather than within the report.

However, when I go to export the report to PDF I receive a ParameterFieldCurrentValueException. I do not receive this error when the data is saved with the report.

I have verified that all five tables in the report are connected to their data source with the following code.

foreach (Table table in reportDoc.Database.Tables)
{
   bool isConnected = table.TestConnectivity();
   Console.WriteLine(table.Name + " connected? " + isConnected.ToString());
}

What might be causing the ParameterFieldCurrentValueException when data not saved with a report and it is exported to disk?

This is the simplest code that can reproduce this issue.

string report =  "list_report.rpt";
string pdf = "list_report.pdf";

ReportDocument reportDoc = new ReportDocument();
reportDoc.Load(report);

// Setting parameters (or not) does not have an effect on the error
// All parameters have defaults
// reportDoc.SetParameterValue("hideRegion", false);

reportDoc.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, pdf);
A: 

This one will not be easy to solve!

What about giving default values to all your parameters before saving? Do you get the same error? If no, you'll then have to try each one of them to find the one causing the trouble. I guess you'll end somewhere with a parameter value used in a formula...

PS: even if this has nothing to do with your problem, I feel confused by the use of the CR keyword 'formula' in your code.

Philippe Grondier
I do get the same error when all fields contain a default value. I've simplified the code needed to reproduce the issue and updated the original post above.
Ryan Taylor
+1  A: 

This answer isn't mine, it is by a gentleman on the Crystal Reports forums. That posting can be found on the SAP Forum The answer there was:

When you have Save Data with Report selected the report does not try to hit the database. It's a static report with saved data. That's why this report doesn't error compared to when you try to pass parameters.

The error suggests that one or more of the parameter values you're passing does not match what the report is expecting. Since you're not viewing the report there's no opportunity for the engine to prompt for missing values.

You can build a simple windows application that loads your report. Start by not passing any parameter to the report without saved data. You should be prompted for all your parameters. Add them manually in the parameter prompt form. Does this work? It should. Now start adding parameter code one at a time. The parameter prompt form should just prompt for the parameters that are missing. At some point you'll get the error again and this should help narrow down the issue.

What I then did was to load the report into a Crystal Report Viewer control. When this loaded it prompted me for any missing parameters I had. I had missed one parameter in a list of 40+ parameters which was causing the error. The code for that follows:

ReportDocument reportDoc = new ReportDocument();
reportDoc.Load(")list_report.rpt";
Form1.SetParameterValues(reportDoc);

this.crystalReportViewer1.ReportSource = reportDoc;

If said person replies to Stack Overflow with this answer I shall select his post as the answer in order to appropriately credit the source.

Ryan Taylor
interesting. Thanks
Philippe Grondier
+1  A: 

Hello Ryan,

When you have Save Data with Report selected the report does not try to hit the database. It's a static report with saved data. That's why this report doesn't error compared to when you try to pass parameters without saved data.

The error suggests that one or more of the parameter values you're passing at runtime does not match what the report is expecting. In your code you're exporting to PDF directly. Since you're not viewing the report there's no opportunity for the engine to prompt for missing values.

You can build a simple windows application that loads your report and passes it to the Crystal Report Viewer control. Start by not passing any parameter to the report without saved data. You should be prompted for all your parameters. Add them manually in the parameter prompt form. Does this work? It should. Now start adding parameter code one at a time. The parameter prompt form should just prompt for the parameters that are missing. At some point you'll get the error again and this should help narrow down the issue.

Sincerely,

Dan Kelleher

Excellent. Thanks for posting. That did the trick!
Ryan Taylor