JasperReports: How to pass parameter to subReport - jasper-reports

How to pass parameter to sub report in JasperReports?
I'm able to pass the parameter to master report but I'm not able pass parameter to its sub report.
Can any one give me the solution for it?

Try using iReport if you are not.
When viewing a report within iReport you can select the properties of the subreport within the main report.
In this there is an option in Subreport properties called Parameters. Using this you can pass parameters to your subreport.
Select this and click the Add
button.
Then click the button right of the
Value expression Box
Next an option screen will pop up
where you can select the parameter
from the master report you want to
pass.
If you want to do it via xml here's a quick example.
<subreport>
<subreportParameter name="XML_ID">
<subreportParameterExpression>
<![CDATA[$P{MASTER_REPORT_PARAMETER}]]></subreportParameterExpression>
</subreportParameter>
</subreport>

Related

jasper report detect if datail band or subreport is present in current page

I'm developing a report with jasper.
The main report has several subreports.
Only one of these subreport has a fixed text with a note number, something like this.
my text<sup>(2)</sup>
In main report I must print the footnote, but only in all the pages in which the text with the note is present.
An image to better explain:
Is there something that permit to detect if a subreport is present in the current page?
Or something that permit to detect if a detail band is present in the current page?
Or something that permit to detect if a particular text will be printed in current page?
Or other strategy to handle this particular situation?
. Create Variable in main report 'subReportCount'
Type = Integer, default 0
Reset Type = 'Page'
. Place this variable (hidden) somewhere after Subreport
. Create variable 'SubVar' in subreport , expression == $V{REPORT_COUNT}
. In main, go to subreport properties -> Edit return Values - >
Set From Varible= SubVar , To Variable=subReportCount, Calculation type = Sum
. Page Footer Print When Expression = $V{subReportCount} > 0
This may work if you are trying if subreport is printed atleast once in page.
Does not matter if you place subreport variable in report or not.
I tried on my local , its working for similar situation
Variable in Main report
<variable name="subReportCount" class="java.lang.Integer" resetType="Page" incrementType="Page" calculation="Sum">
<variableExpression><![CDATA[$V{subReportCount}.equals( null ) ? 0 : $V{subReportCount}]]></variableExpression>
<initialValueExpression><![CDATA[0]]></initialValueExpression>
</variable>
Return Value for the subreport
<returnValue subreportVariable="SubVar" toVariable="subReportCount"/>
Print when expression for Footer element, in my case I am showing Page number
<printWhenExpression><![CDATA[$V{subReportCount} > 0]]></printWhenExpression>
This should work , if not please share your jrxml and sample data.
Good Luck!

Jasper subreport not showing

I am using Eclipse JasperReports Studio and have run into a problem I can't resolve. I have a pretty basic report with detail fields which return about 30 rows. I have a subreport I want to incorporated into the main report but when I add it nothing displays. I have tried adding it to the Column Footer, in the Page Footer with my Page x of x fields, and in the detail field. Which didn't work so well since it added a blank spot between every row retrieved. I have other reports with sub-reports in various bands and they all work fine. Here is the subreport code for the col footer:
<columnFooter>
<band height="110">
<subreport>
<reportElement isPrintRepeatedValues="false" x="0" y="10" width="555" height="100" uuid="34205e71-ec6b-422c-aa27-057678430999"/>
<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
<subreportExpression><![CDATA["The_Report.jasper"]]></subreportExpression>
</subreport>
</band>
</columnFooter>
Any obvious reasons why this won't show up? I don't get any errors and the main report runs fine. The subreport works fine on it's own and it is in the same directory as the main report. I compiled it there and the .jasper file is there as well.
I came across the same problem when using subreports.
In the editor: deleting the content form connectionExpression
And putting $P{REPORT_PARAMETERS_MAP} in the Parameters Map Expression did the trick for me.
Source code: <parametersMapExpression><![CDATA[$P{REPORT_PARAMETERS_MAP}]]></parametersMapExpression>
Verify that the Subreport Expression is pointing to the correct/expected subreport. There are times (often due to copying and/or hand-editing existing JRXML files) where the Subreport Expression is still pointing to the subreport Jasper file of the original, Main report. I just ran into this case, myself.
Make sure that
<subreportExpression><![CDATA["The_Report.jasper"]]></subreportExpression>
isn't supposed to be something like
<subreportExpression><![CDATA["The_New_SubReport.jasper"]]></subreportExpression>
or contain the Subreport directory like
<subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "The_New_SubReport.jasper"]]></subreportExpression>
You should also verify that your subreport doesn't require data in order to display. If it does and your query doesn't return any data, you can do the following:
SELECT FIELD_NAME
FROM TABLE
WHERE KEY = $P{KEY}
UNION ALL
SELECT NULL AS FIELD_NAME
FROM DUAL WHERE (
SELECT COUNT(*)
FROM TABLE
WHERE KEY = $P{KEY}
) = 0
A quick reminder, after double checking that you have the correct values for all the <subreportExpression> elements. Make sure you Build All the project files.
The main report will call the compiled version of the subreport. So once you have tested and saved your subreport, click the Build All button or click on the Menu option under the 'Project' menu at the top of the window.

iReport parameter not being informed by field

I am using iReport - my first time using it - and I am trying to make a report that will involve passing a parameter to a subreport. I am not able to get that to work, so I did the following test:
I have a large query that grabs various Fields from my DB - when I preview the report these fields are being pulled and display (over and over, etc., a lot of stuff is being pulled).
I created a new parameter on the main report called "myparam". I made it's default value one of the fields that is being successfully pulled from my DB.
I created a textfield to display this param - the intent being it will echo the field that displays already in my report. If I can get it to do that, I figure I can get it to correctly "plug in" to my subreport.
The issue is - when I preview, I am asked to give the parameter a value - I choose default, which defaults it to the Field I set, which means it should now echo that field (aka I WANT it to echo that field). But it does not echo that field. Instead it just shows null over and over again. Here is a screenshot of what I am talking about
Here is the setup for my report:
Here is proof I've set the default value in my xml:
<parameter name="myparam" class="java.lang.String">
<defaultValueExpression><![CDATA[$F{TCORDERID}]]> </defaultValueExpression>
</parameter>
Here is a snippet of the report which is being generated incorrectly (aka myparam is not getting populated right).
THEY ARE BOTH STRINGS
From my understanding of parameters, they are passed to the report/subreport when it gets created. You have set the default value of the parameter to one of your fields, which has not been populated yet when the report gets generated.
I would suggest adding a simple subreport (which is the reason you want to use parameters anyway), remove the parameter on the main report, and add it to the subreport (with a text field to display it).
Now, add the Parameter to the subreport object in your main report. (This is under the Subreport properties section in iReport.
Name: The name of the Parameter in the subreport.
Expression: The field from your main report that will be passed to the subreport.
So your main report will have the following under the subreport detail:
<subreportParameter name="myparam">
<subreportParameterExpression><![CDATA[$F{TCORDERID}]]></subreportParameterExpression>
</subreportParameter>
And then the Parameter in the subreport:
<parameter name="myparam" class="java.lang.String">
<defaultValueExpression><![CDATA["No result"]]></defaultValueExpression>
</parameter>

How to pass the parameter from one subreport to another subreport

I am newbie and I'm designing my report using iReport 4.5.
I have a main report (MainReport) and three subreports (Sub1, Sub2, Sub3)
In Sub1 I have three summary variables say presentPayable, presentPayment, balance
In Sub2 I have one summary variable say totalCost
I need to use the summary variables of Sub1, Sub2 in my Sub3.
How can I do this? Is this possible to pass the variables from one subreport to another?
Otherwise please provide me any alternate to do this.
To pass a value from a subreport to its parent, the parent must first have a variable to receive the value. In your case the main report should have 4 variables, one each for presentPayable, presentPayment, balance, and totalCost.
Next you need to add a returnValue element to the subreport element in the main report. This element maps a variable in the subreport to a variable in this report using the attributes subreportVariable and toVariable.
To do this in iReport, click on your subreport element in the main report. In the properties list, click on Return Values. A dialog should appear. Click on the Add button. Type the name of the subreport variable and select the variable in this report that you would like it to be transferred to. You should leave the calculation type as "Nothing", which will instruct jasper to simply overwrite the variable with the new value. Click Ok to add this, then repeat for the other variables/subreports.
Now when you run the report, each time the subreport has completed processing, the current value of the variable in the subreport is passed back to the specified variable in the main report.
To use that value in another subreport, you need to pass the variable from the main report to the other subreport as a parameter. This has two parts: Adding a subreportParameter to the subreport element in the parent report, and adding a parameter to the subreport itself.
In iReport, click on your subreport element in the main report. In the properties list, click on Parameters. In the dialog that appears, click the Add button. Give the parameter a name (e.g. presentPayable) and input a value expression that references the variable in your main report (e.g. $V{presentPayable}). Repeat this for each of the variables that you want to pass in.
Next, open your subreport. In the report inspector, right-click on Parameters. Select Add Parameter, then rename the new parameter to match the name you entered in the previous step.
In the subreport, you should now be able to reference these values like any other parameter (e.g. $P{presentPayable}).

Passing JRBeancollection datasourse to master report and subreport

I want to pass the JRBeancollection datadourse to main report and from main report to subreport.. I have included this subreport in summary section of master report..
please mention the steps i want to do in ireport including the inbuld parameters how to set.
Finally I have done it.
Actually datasource was not going in subreport. So I have changed the property of when no data to all sections, no data...
Then I have passed the collection variable where I have fetched the subreport records.
By using parameters.put("shape_data_all", allShape_Caliberation);
Then I have created a parameter in main report. And changed its class to java.Util.Collection.
Then in subreport property, connection type= Use a datasource expression
Data Source Expression new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{shape_data_all})
Then added the fields in the subreport same as I am having the bean properties.
Thank you.
I'm not sure, but try this:
<subreport>
<reportElement x="261" y="25" width="200" height="100"/>
<dataSourceExpression><![CDATA[$P{REPORT_DATA_SOURCE}]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "subreport.jasper"]]></subreportExpression>
</subreport>
Of course you have to use the "subreportExpression" as you are doing in your report, this is just a sample, the useful line for your problem is where the "dataSourceExpression" is placed.