Handling Optional Parameters when running Crystal Reports 2011 report from C#

淺唱寂寞╮ 提交于 2019-12-11 06:58:50

问题


I'm writing a custom scheduling tool in C# using VS 2010, to execute and export a number of Crystal Reports 2011 rpt files. Currently these reports are executed manually from within CR Developer, and some the reports have optional parameters, which can be left blank at runtime, and these are handled with the CR "HasValue()" function in the record selection formulas within the reports.

I have developed a prototype application to execute and export one of these reports, and can pass in parameter values with no problem:

                // Set up the Parameter/s

        ParameterFieldDefinitions crParameterFieldDefinitions;
        ParameterFieldDefinition crParameterFieldDefinition;
        ParameterValues crParameterValues = new ParameterValues();
        ParameterDiscreteValue crUserProfileValue = new ParameterDiscreteValue();

        crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields;
        crParameterFieldDefinition = crParameterFieldDefinitions["UserProfile"];
        crParameterValues = crParameterFieldDefinition.CurrentValues;

        crParameterValues.Clear();
        crUserProfileValue.Value = "SIPRECLK";
        crParameterValues.Add(crUserProfileValue);
        crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);

This provides the required results in the report PDF and XLS exports.

However this UserProfile parameter is flagged as optional in the report, and I cannot work out how to set it to a blank value, to emulate the user leaving that parameter field blank when the report is executed manually from with CR Developer.

I've tried setting the crUserProfileValue.Value = "", and String.Empty, but both of there are taken as actual parameter values, and when used within the Record Selection Formula, fails to match any record from the datasource and results in an empty report.

If I set the crUserProfileValue.Value = null, then an exception is thrown at:

    crParametersFieldDefinition.ApplyCurrentValues(crParameterValues);

stating "The types of the parameter field and parameter field current values are not compatible."

I've also tried not adding the crUserProfileValue to the crParameterValues collection, but then experience a "Missing parameter values" exception when the report is executed.

How can I programmatically set the value of a Parameter that is defined as Optional in the report to a value that will cause the CR HasValue() function to return FALSE in the Record Selection Formula?


回答1:


OK, so I've eventually found out, via the SAP Community Network, exactly how to set an optional parameter to a "non-value" programmatically.

Rather than using the report's DataDefinition.ParameterFields route, as coded above, I simply need to write:

    cryRpt.ParameterFields["UserProfile"].CurrentValues.IsNoValue = true;

Works perfectly, triggering the required code path in the Record Selection Formula.



来源:https://stackoverflow.com/questions/26055997/handling-optional-parameters-when-running-crystal-reports-2011-report-from-c-sha

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!