Skip to Content
0
Former Member
Feb 09, 2012 at 04:45 PM

Best way to reliably set report parameters?

60 Views

We use a central controller for all of our 350 reports. The reports may be simple, or they may be complex and feature multiple subreports. The parameters differ by report. However, we have some common parameters that may or may not be in most reports. The calling application does not have to set them as the controller will set them automatically. My question is simple, what is the best way to reliably set parameter values in a generic way for main reports and subreports?

There's numerous ways to accomplish this, but all seem to have faults...

// imagine we have these objects

ReportDocument report;

string name;

object value;

// Can set this way for the report if this is a main report.

// 1. This throws an exception if the parameter name does not exist.

// 2. Seems to support subreport parameters.

report.SetParameterValue(name, value);

// Can set this way for the report, too.

// 1. This throws an exception if the parameter name does not exist.

// 2. This throws an exception if the parameter is linked.

// 3. I believe this sets parameters in subreports.

ParameterFieldDefinition parameter = report.DataDefinition.ParameterFields[paramName];

ParameterValues paramValues = parameter.CurrentValues;

ParameterDiscreteValue newValue = new ParameterDiscreteValue();

newValue.Value = paramValue;

paramValues.Add(newValue);

parameter.ApplyCurrentValues(paramValues);

// Can set this way, too.

// 1. Does not throw an exception.

// 2. Cannot be used for subreports!

// 3. Cannot set subreport values!

ParameterField parameter = report.ParameterFields[paramName];

if (parameter != null)

{

ParameterDiscreteValue pdv = new ParameterDiscreteValue();

pdv.Value = paramValue;

parameter.CurrentValues.Clear();

parameter.CurrentValues.Add(pdv);

}

To me, report.SetParameterValue should be the chosen way to set the parameter. However, it should never throw an exception. If a parameter does not exist in the report, it should ignore it. Am I crazy? What's the correct answer? Thanks!

Kyle

P.S. There's no great way to identify parameters that are in an entire RPT (main report and subreports). I have to write this code to find out. All other options throw exceptions or don't consistently return results if there's a subreport:

private bool ReportContainsParameter(ref ReportDocument report, string name)

{

foreach (ParameterFieldDefinition parameter in report.DataDefinition.ParameterFields)

{

if (String.Compare(parameter.Name, name, StringComparison.OrdinalIgnoreCase) == 0)

{

return true;

}

}

return false;

}