title: Handling Optional Parameters when running Crystal Reports 2011 report from C#
body: <p>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.</p>
  
  <p>I have developed a prototype application to execute and export one of these reports, and can pass in parameter values with no problem:</p>
  
  <pre><code>                // 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);
  </code></pre>
  
  <p>This provides the required results in the report PDF and XLS exports.</p>
  
  <p>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.</p>
  
  <p>I've tried setting the <strong>crUserProfileValue.Value = ""</strong>, and <strong>String.Empty</strong>, 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.</p>
  
  <p>If I set the <strong>crUserProfileValue.Value = null</strong>, then an exception is thrown at:</p>
  
  <pre><code>    crParametersFieldDefinition.ApplyCurrentValues(crParameterValues);
  </code></pre>
  
  <p>stating "The types of the parameter field and parameter field current values are not compatible."</p>
  
  <p>I've also tried not adding the <strong>crUserProfileValue</strong> to the <strong>crParameterValues</strong> collection, but then experience a "Missing parameter values" exception when the report is executed.</p>
  
  <p>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?</p>