Pass values from main report to sub report

ireportjasper-reports

I just started to work with iReport and got the task to do some changes to already existing reports.

Currently there's a main report with section A and B (contact details and some other info). There is a sub report C and then a subreport D. My task is to replicate A and B for every item in D. If the report has many D sections I want to have an A and B for each.

All data is in the same XML document and to get the data for section A and B I have simply

((net.sf.jasperreports.engine.data.JRXmlDataSource)$F{REPORT_DATA_SOURCE}).dataSource("/Header/")

and for sub report with D section

((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("/Header/ProductHeader/Member")

if I drag a field from Document Structure that displays data in Section A to section D and check the expression it says
$F{ContactNumber} but displays Null in D even though it displays a value in section A

How do I need to modify my data sources to display content of A and B in D?

Thanks in advance

Best Answer

You need to pass parameters to your sub-report. A sub-report doesn't share any data from your main report unless you explicitly define the contract. It is more like a method call then a continuation of the main report.

If you wanted to display a field with the name foo in your main report in the sub-report you would need to do the following:

  1. Create a parameter in your sub-report with the name foo.
  2. Create a sub-report element in your main report.
  3. In the Properties of the sub-report element, there is a property called "Parameters". Open up the dialog and add a parameter with the name foo and the correct value expression.

When you want to use the value for foo in your sub-report you should use the expression: $P{foo} if it is a main report parameter.

Related Topic