7

Passing Report Parameters to SubReport in VS 2010 RDLC

I'm having some troubles defining and passing report parameters to subreports in VS 2010. In VS 2008 in the design view I was able to right click and define the report parameter and have it passed through.

In VS 2010 that prompt is missing. So my question is, how can I pass a value from a parent report to a subreport in VS 2010?

Additionally, this is what is shown in the Report Properties dialog inside of VS 2010:

For the time being I have defined the parameter in the subreport manually in the XML but I'm receiving an error from the main report when I attempt to pass a parameter of any type to the subreport.

The error is :

An error occurred during local report processing.

Value cannot be null. Parameter name: value

Where I do not have a parameter named value defined anywhere.

Prodip
  • 91
  • 1
  • 1
  • 3

1 Answers1

22
  • Goto SubReport -> Report Data Pane -> Parameters and add the parameter you want to receive.

  • Goto MainReport -> Right-click SubReport -> Subreport Properties -> Parameters and add the same paramter name and pick the relevant value from the dropdown.

  • Handle the SubreportProcessing event and set the datasource for the subreport. In my case the main report datasource was of type List<Order> and the parameter was OrderID. Sample code below.

ReportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(SetSubDataSource);
public void SetSubDataSource(object sender, SubreportProcessingEventArgs e)
{
    var mainSource = ((LocalReport) sender).DataSources["MainDataSet1"];
    var orderId = int.Parse(e.Parameters["OrderID"].Values.First());
    var subSource = ((List<Order>)mainSource.Value).Single(o => o.OrderID == orderId).Suppliers;
    e.DataSources.Add(new ReportDataSource("SubDataSet1", subSource));
}
Ε Г И І И О
  • 11,199
  • 1
  • 48
  • 63
  • 1
    **Very Important** The name of the `ReportDataSource` in `e.DataSources.Add` method must match the name of datasource in subreport EXACTLY. –  Jun 02 '16 at 12:14
  • An alternative to defining subSource like that is using this tag: This will do the same thing and save you those 3 lines of code too. – Talha Imam Mar 15 '17 at 11:36
  • 1
    If you have multiple subreports and datasources, you'll want to ensure `e.Parameters` isn't null and (if you have multiple params), ensure you're processing that specific report via `e.RportPath`. In my case, there were a dozen subreports and the param was set on a subreport and used in a child subreport, so all other subreports showed `e.Parameters` as empty. – Alex Mar 07 '19 at 15:10