Skip to Content
author's profile photo Former Member
Former Member

Best way to reliably set report parameters?

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;

}

Add a comment
10|10000 characters needed characters exceeded

Related questions

1 Answer

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.