Passing Report Parameters to SubReport in VS 2010 RDLC

Prodip picture Prodip · Sep 12, 2011 · Viewed 25.1k times · Source

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.

Answer

Ε Г И І И О picture Ε Г И І И О · Jan 19, 2012
  • 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));
}