IReport / JasperReports – Adding a subreport in the main report and using main parameters in subreport
Finally i spared myself some time to upgrade my Jasper Server to version 3.7.5 on Glassfish V 3.0.1, and as a consequence, i got to (finally) set up the 3.7.5 version of IReport on my Windows 7 machine.Back to the Jasper World…i’ve been missing it!
The last thing i covered on IReport and JasperReports, (other than configuring Jasper Server for Glassfish V3) was how one can use main dataset parameters in subdatasets. One of you blog readers (Brad) asked the question:
“But what if I don’t want to use my subdataset for a chart/graph? I just want to use the main report’s parameter in the subdataset query.”
This is what i am going to talk about now
Step 1 – Add a subreport in the Jasper Repository
In order to add a subreport in your main report, you have to design the subreport first, and it has to exist, as a independent entity in the Jasper Server Report Repository. I used the following structure:
- Main Reports(Folder)
- Response Times Report (Report)
- Subreports (Folder)
- statistics (Report)
Step 2 – Add a subreport to the main Jasper Report
Open the main report. Select the “Subreport” element from the Report Elements, and draw it on your main report
You now have to link the subreport element that you just added to the main report, to your subreport file (the one you created in step 1) In order to do that, select the subreport element in the main report, and modify its parameters as follows:
- Subreport Expression: “repo:statistics”
- Expression Class: java.lang.String
- Connection Type: Use a connection expression
- Connection Expression: $P{REPORT_CONNECTION}
Step 3 – Adding and mapping the parameters between the main report and the subreport
The “Response Times Report” contains our subreport called “statistics”. The statistics report is supposed to calculate and show in a table format the following values:
- the Request Name
- total transactions
- average response time
- standard deviation
- 50 to 90 response time percentile
In order to calculate this values, we need to know some information like:
- the build id (software package version)
- scenarioname (the name of the test scenario)
- architecture name ( the type of test: baseline, performance, load)
- runid (the id of the testrun)
We will get these parameters by passing them from the main report to the subreport. The rule is, for every parameter we want to pass to a subreport, a similar parameter (with another name) has to exist in the subreport, so that we can make the mapping. We discussed about four parameters that we need from the main report:
Parameters in Response Times Report (Main Report) : build_number, test_scenario_name, architecture_name,run_id
Parameters that we NEED to define in the Statistics Report (Subreport): buildnumber, scenarioname, architecturename,runid
This is how the subreport looks like (including the defined subreport parameters; note: the currently open report is the “statistics” subreport, and the values in the left column are the “subreport parameters”)
After we added the parameters in the subreport, all we need to do is to map the main parameters to the “subparameters” As you see in the image above (step 2), the Parameters property shows “4 properties defined”. Let’s open the Properties view of the subreport once more, and do the mapping ( note: the currently open report is the main report, and the values in the left column are the main parameters):
As you can see, the “name” column accounts for the main report parameters, whereas the “expression” column accounts for the subreport parameters. We are now ready to use the “passed parameters” in our subreport.
Step 4 – Using the passed / mapped main report parameters as subreport parameters in a query
This is the simplest part: you just need to use the subreport parameters in the “FROM” condition of your query. Example below:
select …..from….where info.build=$P{build_number} and td.scenario_id=$P{test_scenario_name} and td.architecture_id=$P{architecture_name}
ADDITION – IMPORTANT
After looking over some of the questions that some of you asked, it ocurred to me that the example above, where the subreport resides in another folder, requires an additional setting.
If for example your main report resides in the “main reports” folder, and your subreport in the “subreports” folder (where “main reports” and “subreports” reside at the same level in the folder tree, under “reports” root folder) you will have to add the subreport as a resource reference, like in the picture below:
That’s it. Time for week-end. Enough work, blogging. Time for family
Cheers,
Alex





hi,
the subreport element should be put in detail band or where? coz i put it in detail but it keep repeating the data..
hi faiza,
i do not really know what you mean by repeating the data. Anyway, i have not used this in the detail band, since i needed it as a statistical report in the title band. Still, i do not think that this should be a problem, regardless of where you put it.
Can you show me a print of your report?
alex
hiii faiza….
I got the same error…Have you solved it???
hi,
i tried your tips and my report is still not worked.
All I want is my sub report is using pass data from main report and the query is from mysql database. But my main report is using datasource from my java program.
In my java program, all element in my main report is showing but element in sub report is not showing. It only shows the header of my sub report.
Is there any suggestions?
if i pass my datasoure from my backing bean to sub report similar main report. i don’t use connection any DB on ireport. how can i call and view sub report, how can sub report know parameters pass throught main report. i didn’t saw any error when conpile. in ireport i design template and didn’t use any connection. list object only pass in backing bean. main report call sub report by
* Subreport Expression: “repo:ProblemSub”
* Expression Class: java.lang.String
* Connection Type: don’t use connection or datasource
* Connection Expression:
I have the same question than rifqi, I pass data from a Java program. I did everything of you said except the last query (I dont use DB). My main report works well, but I can´t see subreport parameters. Is there anything we can do?
Thanks.
Hi Adrian, Rifqi, Duong
i have ammended the posting with some additions. It looks like i have omitted the part with the subreport reference.
Please let me know if that solves your problem.
Alex
I have the same question the faiza.
I have a Main Report with a subreport element in detail band. The Sub Report File has a detail band, with the fields I like to print. The problem: My list (from a java program) has 10 itens. The report is printing 100 itens (10×10).
Can any body help me on this please.
I created my report using iReport 3.7.4 and it has 6 sub reports. It is working fine in iReport.
But the issue is we I add it into my application and call it through Java code it does not gives me any out put prompt 4 alert boxes as “Document has no pages”. But when I use thee same parameter value in iReport it worked, and still working.
If anybody knows the reason please give me a help.
Thanks
Faiza and rafeal,
You both need to put a condition in your sub reports to get rid of repeating values.
If your header report has x values then your sub report will execute for x times, thats why it creates x*x number of records in your subreport.
Hi there virajZ
I’m having a problem with repeating values when I execute my main report.
I have unchecked ‘Print Repeated Values’ under the subreport properties, yet the values are still repeating.
Please could you explain to me how to fix this?
I have the same question the rifqi & virajZ.
I created my report using iReport 3.0.0 and it has 1 sub reports, It is working from ireport, whenever i run this from my application it’ll give exception like, COULD NOT LOAD THE OBJECT FROM THE LOCATION :.\classic_subreport0.jasper
hi Parveen,
Mostly your exception is because the subreport path is wrong.
I’m not sure this is correct, in ur error massage subreport path is strange.
normally it is like ./classic_subreport0.jasper instead of .\classic_subreport0.jasper.
But if you give that path manually try to change it to ./classic_subreport0.jasper.
If so i recommend you to use default subreport parameter instead of putting ‘./’.
My problem is different that is has neither exception in java application nor in iReport.
virajZ,
I tried to specify the full path of subreport(i.e., C:/Program Files/JasperSoft/iReport-3.0.0/classic_subreport0.jasper) but its giving null pointer exception.
Hi Parveen,
If it has null pointer, I believe it can be fix. Copy whole error msg here. I’ll give you an answer.:)
Regards virajZ
Hi virajZ,
Here is my exception,
java.lang.NullPointerException
at net.sf.jasperreports.engine.JRPropertiesMap.readObject(JRPropertiesMa
p.java:185)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readArray(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:88
)
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:64
)
at erpproject.Sample.jButton1ActionPerformed(Sample.java:107)
at erpproject.Sample.access$000(Sample.java:32)
at erpproject.Sample$1.actionPerformed(Sample.java:57)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Sour
ce)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown
Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown
Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown
Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Hi virajZ,
I got the solution, now my sub report is successfully executing from my application, but the problem is with the sub report parameters, I’m not getting how to pass parameters to sub reports from java code.
virajZ,
My sub reports are working fine with parameters too.
The solution for your problem is, you have take same parameters in master report as sub reports, then u’ve to set the default expression of sub report paramter as master report parameters(i.e., if u have $P{date} in subreport then create a parameter $P{Masterdate} in master report and then go to subreport properties and set default expression of $P{date} as $P{Masterdate}) . I think this will work.
Hi Parveen,
Thank you very much for your comments. I’m glad to hear that you solved your problem.
How ever I fixed my previous issue, but it is not using a convince way. I think i’ll try to do wht you said and get back to you once done.
Thanks,
virajZ.
but i faced a serious problem,that is “when master report not satisfied with given parameters then subreport also not displaying with data eventhough subreport contains that given parameters”.
i will appreciate if any one solve this kind of problem…..
I am doing it as you mentioned, however I need to pass the field $F{work_id} to my subreport. Do i have to set the parameter equal to this field and then pass the parameter to my subreport?
Hi michael,
No need to assign ur field to parameter and pass to the subreport.
1). Make sure you create all the parameters you need in subreport.
2). then add your subreport into the main as usual and then you can assign your main report values to subreort parameters.
3). goto subreport properties select parameters.
4). In there you will see add, modify delete button if you use iReport.
5). If there got anything unnecessary delete them and, press add button.
6). then type your subreport parameter name correctly and assign the main report parameter, field or anything that you need from the main report using the popped up dialog box.
7). it is the same wway that you pass parameter to a subreport from main report…
this will do …
virajZ
guys, need help…
Im currently working on a project with jasper report. it seems that the pdf file (report) runs on a browser succesfully without a subreport in Glassfish server but when there is a subreport the error occurs. ive been working with this problem since last week and im already desperate. The project has no problem deploying through NetBeans but when I run the war file on our web server there is a “java.lang.IllegalArgumentException
here’s the root causes in glassfish log folder:
[#|2011-01-31T13:48:52.117-0800|WARNING|glassfishv3.0|javax.enterprise.system.container.web.com.sun.enterprise.web|_ThreadID=24;_ThreadName=http-thread-pool-8080-(1);|StandardWrapperValve[guestFolio]: PWC1406: Servlet.service() for servlet guestFolio threw exception
java.lang.IllegalArgumentException: name
at sun.misc.URLClassPath$Loader.findResource(URLClassPath.java:460)
at sun.misc.URLClassPath.findResource(URLClassPath.java:146)
at java.net.URLClassLoader$2.run(URLClassLoader.java:385)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findResource(URLClassLoader.java:382)
at java.lang.ClassLoader.getResource(ClassLoader.java:1003)
at java.lang.ClassLoader.getResource(ClassLoader.java:998)
at
………………….
Azizo,
please read the “ADDITION – IMPORTANT” part of this document. It is likely that you have forgot to add the subreport resource as a resource reference. Please keep in mind there has to be a resource synchronization between ireport (or your local repository) and jasper server repository.
Let me know if it works
i have a different problem than you all before, i have one main report which works OK and one subreport that also works OK.
when adding the subreport to the main report it does work, but the issue i have is subreport only prints once in main report.
so if i have 3 rows of data in main report, and 5 rows of data in sub report i get printed:
1 row of main
—-5 rows of sub
1 row of main
—-nothing
1 row of main
—-nothing
any help ???
just to add to the post: my subreport is using a datasource that I send to the main report as a parameter.
It’s so clear. Thanks.
pleas pleas i want a help …….
i m designing a jasper report and i find a problem …. i want to make report to projects of students in university and there is more than one student in each project and i should print their profiles (profiles of students in sequence) then i will print information about the project
but my problem is when i print the profiles of each student i should print ALL ALL ALL of his books so how i can make it ….please how can help me i can send the files to his email if he didn’t understand …my email : kareem-tahomi@hotmail.com
any 4.5 ireport tutorials? i never found it :\
Hi,
I have a main report. Then a subreport_1 in the Summary band of my main report. And a subreport_2 in the summary band of my Subreport_1. The main report and subreport_1 will generate multiple pages. Subreport_2 is a single page report. When i run these reports, I get page numbers 1, 2, 3… for Main report pages. Then for subreport_1, the page number starts from 1 again and goes on like 1,2,3…. And for subreport_3, the page number is again reset to 1. But i want continuous page numbering for all 3 reports. Because , the output report will be a single pdf report. please help me on how to fix this problem. I am displaying $V{PAGE_NUMBER} in the page header of all 3 reports with evaluation_time “NOW”.
for a subreport there is always a dataset required?
I would like to pass only parameter from the main report which does not need any data connection, to create subreports for header, footer etc. Is this possible?
I just can’t get it working :/
Great tutorial, Alexandru. I’ve run into an unexpected issue when trying to add the resource reference, and I wonder if you might have some thoughts? Here is the link to my post at jasperforge that has all the details: http://jasperforge.org/plugins/espforum/view.php?group_id=112&forumid=102&topicid=97551
Great article Alexandru, I wonder if you have some thoughts on a problem I am having when trying to add the resource reference? The details are in my post at jasperforge here: http://jasperforge.org/plugins/espforum/view.php?group_id=112&forumid=102&topicid=97551
Hi There,
In iReport4.5 if I use the “repo:statistics” as stated in this example I am getting Unable to locate subreport with expression “repo:statistics” followed by java.lang.Exception: repo:statistics not found.
In my case I have both the main report and the subreport residing in the same folder.
All works good if I pass the full path in the following
How do I resolve this issue….
Thanks for your help with this…
Meeza
Hi,
I am getting problem in iReport 4.5 export to excel. When jrxml is exported to excel the size of the text filed is going out of the cell size but it should be of the same size of the cell.I need immediate help with this.I am not able set any property and not able to solve this problem.
Please need immediate help and response is appreciated.
Thanks,
Swathi.
Hi swathi,
i am sorry, haven’t exported to excel yet. I hope i will find the time to take a look into that. Maybe some other blog followers can help
Alex
Thank you for this post. I’m using iReports again after not using it for about 6 months. This was a good refresher on subreports and parameters.
Sarah, welcome back. I tend to read it myself every now and then when i resume working with IReport
Glad it was of help.
Hi Alexander, first of all thanks for the great post! It helped me a lot! I still got a question though. I’m trying to also pass a parameter of the type Collection to the subreport but I think it is getting ignored. The subreports are empty when I use this parameter in the query. I posted a “detailed” explanation on the forum but no answers yet unfortunately.
http://jasperforge.org/plugins/espforum/view.php?group_id=83&forumid=101&topicid=98616
Thanks for your help…
Cheers,
Tijmen
Hi Tijmen,
May be I can give you a small help if you show me the way you query out that Collection “vendor” in your header. I feel something is wrong the way you passing it to the Subreport and the way you use it in subreport, but not sure I’m correct.
HiTijmen,
i have tried to answer your question in a detailed post. Give it a try and let me know.
http://alexandru-ersenie.com/2012/06/22/ireport-jasper-reports-working-with-subreports-and-collections-in-jasper-reports/
virajZ is also really good in specific problems like this, can highly recommend
Hi Alexandru,
Thanks for the recommendation, appreciate it.
And appreciate all this guidance which help us a lot to improve ourselves.
virajZ.
hi All , i am getting error please tell me,any one get solution
java.lang.Exception: repo:subreport not found. at com.jaspersoft.ireport.designer.utils.Misc.locateFileFromExpression(Misc.java:1374) at com.jaspersoft.ireport.designer.compiler.IReportCompiler.compileSubreports(IReportCompiler.java:1437) at com.jaspersoft.ireport.designer.compiler.IReportCompiler.run(IReportCompiler.java:519) at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:572) at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:997)
Hi friend, i have using jasper iReport 4.7.0, i need two page of pdf file that means i want to prepare two page of one pdf file. what am saying is i got out put for one page pdf but not getting like this, i want second page also with in that same file and want to show two page of one pdf file…… how i getting this output anybody give idea…. am expecting answer from Mr.Alex…..
Thanks
Uduman
Hi there virajZ
I’m having a problem with repeating values when I execute my main report.
I have unchecked ‘Print Repeated Values’ under the subreport properties, yet the values are still repeating.
Please could you explain to me how to fix this?