1

In C# I'm trying to set the report parameters for an SSRS rdlc file. First of all, am I doing this correctly?

  1. GetParameters() to return a collection of the parameter info
  2. Create a list of new ReportParameter objects, assigning name and values
  3. Call SetParameters with the new ReportParameter list as the argument.

Assuming I'm handling this correctly, here's my conundrum. Code is vastly simplified for this example:

var reportParams = new List<ReportParameter>();
foreach (var param in report.GetParameters()) {
    reportParams.Add(new ReportParameter(param.Name, "1"));
}
report.SetParameters(reportParams);

When the report parameter type is an integer, I get the following error on the SetParameters call:

The value provided for the report parameter 'intActualWeight' is not valid for its type.

But when I try to set the parameter value as an integer:

reportParams.Add(new ReportParameter(param.Name, 1));

The IDE says it cannot convert from int to string. Looking at the overloads for new ReportParameter, it is expecting a string or string array.

What am I doing wrong? Thanks in advance!

Steve.

  • If its an `int` add it as `reportParams.Add(new ReportParameter(param.Name, 1));`, i.e., drop the `"`. – Olivier Jacot-Descombes Jul 12 '18 at 19:51
  • But as I pointed out in my question, when I do that the IDE barks at me that it can't convert from int to string. – user2441279 Jul 12 '18 at 19:55
  • 2
    This may or may not be useful but in a lot of cases the report parameter can be defined as test even if it's really an int value. I do this all the time and never have an issue. So you may be able to just change it in the report design then always pass it in as a string – Alan Schofield Jul 12 '18 at 19:59
  • Alan, not a bad idea. We have a dedicated resource for report creation, so I've asked if that's feasible. We'll see what his reply is... – user2441279 Jul 12 '18 at 20:07
  • Alan, do you want to 'officially' answer this question with your suggestion? That worked perfect! – user2441279 Jul 16 '18 at 23:55

1 Answers1

0

Heres what worked for me:

int paramName = 1;
string paramNameAsString = paramName.ToString();
ReportParameter[] reportParameter = new ReportParameter[1];
reportParameter[0] = new ReportParameter("ParamName", paramNameAsString);
ReportViewer1.ServerReport.SetParameters(reportParameter);
ReportViewer1.ServerReport.Refresh();
minTwin
  • 1,181
  • 2
  • 21
  • 35