[Solved] Problem passing datasource to subreport

1

Hello all, I am trying to build a report from my java application that uses subreport. I built the fields in my subreport with the same names as the ones in my java bean, but when I pass the master datasource to the subreport ($P{REPORT_DATA_SOURCE} in the datasource expression) the subreport fields are shown null to me. But I can see the field value if I set it in the master report. Maybe I am missing something along the way or setting the datasource in a wrong way? Any help is appreciated :) Using iReport 3.0.0 but tried 3.7.0 and results were the same.



Post Edited by mahoushi at 05/18/2010 12:39
mahoushi's picture
Joined: May 4 2010 - 11:21am
Last seen: 8 years 10 months ago

9 Answers:

0

Have you taken care of mapping master report parameters mapping with sub report parameters. The mapping is a must to pass values from master to sub-report. Hope this info info helps you?

rakeshenoch's picture
Joined: Oct 14 2009 - 12:19pm
Last seen: 9 years 5 months ago
0

I set the REPORT_PARAMETERS_MAP in the subreport parameters section. With this I can access all parameters from the master report in the subreport, no problems in there. Now what I need to do, is to get the same datasource from the master report in the subreport. I tried setting the subreport datasource expressions as $P{REPORt_DATA_SOURCE}, but still couldn't read the field I wanted, so I tried setting the datasource in my java code, as a parameter, then I set the datasource expression as $P{mydatasource}, but still the field value in my subreport returns null.

Any ideas?

mahoushi's picture
Joined: May 4 2010 - 11:21am
Last seen: 8 years 10 months ago
0

I use this: 

Connection type: use a connection expression.

Connection Expression: java.sql.DriverManager.getConnection("<Connection>","<UserName>", "<Password>")

 

 

backlash's picture
Joined: May 4 2010 - 5:29am
Last seen: 8 years 10 months ago
2

I am using a java bean describing the fields I need to show in the subreport. Then I created a JRBeanDataSourceCollection with this bean (called dataSource), and then added the datasource to the report as a parameter, as in the following code:

JRBeanCollectionDataSource jrDataSource = new JRBeanCollectionDataSource(dataSource);
String pathToRel = request.getSession().getServletContext().getRealPath(pathToReport+jasperFileName+".jasper");
parameters.put("mydatasource", jrDataSource);

Those fields in the java bean are the ones I need to read in the subreport, and therefore why I am trying to add the mydatasource parameter as a datasource to the subreport.

But still, all I get are null fields... any ideas?

 

mahoushi's picture
Joined: May 4 2010 - 11:21am
Last seen: 8 years 10 months ago
0
I would suggest not to pass data source as a parameter as you've mentioned.
If you want to show 2 different lists with objects in 2 sub reports try to
create traditional Java bean which has 2 collections of needed objects.

For instance,
class JavaBean {
private List<JavaBean1> firstList;
private List<JavaBean2> secondList;

// Getters and setters.
}

Then use traditional approach for fulfilling of report with list of Java Beans.

Then in sub report element create a data source in a way:
new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{firstList}).

That's all. In the sub report you can use properties of JavaBean 1.
The same for the second sub report.

Hope that will help.

Best,
Taras Matyashovsky

tmatyashovsky's picture
Joined: May 30 2009 - 2:53am
Last seen: 9 years 9 months ago
0

In the end I created an arraylist with just one element, the bean I wanted as a datasource for my subreport, like this:

ArrayList<Object> data = new ArrayList<Object>();
data.add(myBean);
JRBeanCollectionDataSource DS = new JRBeanCollectionDataSource(data);
parameters.put("formData", DS);

Then I added a parameter to my master report called "formData" of the exact same type as a datasource parameter.

The problem I was having was because I was trying to do code above in a separate method, and for some wierd reason it didn't work. So I just left all in the same method that create the other parameters, and it did the trick.

mahoushi's picture
Joined: May 4 2010 - 11:21am
Last seen: 8 years 10 months ago
1

Hi

I have been wondering How iReport takes care of subreport I faced all above problem doing subreport in iReport.

In order to pass List / collection to subreport following things need to be done....

In Main Report. >>>>>>>>>>>>>>>>> Put Following

     <import value="net.sf.jasperreports.engine.*"/>
    <import value="net.sf.jasperreports.engine.data.*"/>

//Define List

    <field name="addresses" class="java.util.List"/>

Pass List to subreport

<subreport isUsingCache="true">
<reportElement x="0" y="39" width="555" height="276"/>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{addresses})]]></dataSourceExpression>
<subreportExpression class="java.lang.String"><![CDATA[$P{SUBREPORT_DIR} + "report1_subreport5.jasper"]]></subreportExpression>
</subreport>

 

In Subreport >>>>>>>>>>>>>>>>> put Following

very important >>> below address is field in my addressess List in Main Report

    <field name="address" class="java.lang.String"/>

 

and In Detail Section Put Following

            <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                <reportElement x="142" y="20" width="92" height="20"/>
                <textElement>
                    <font size="12"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$F{address}]]></textFieldExpression>
            </textField>

Remeber Many time you might not see subreport visible in main report reson being it the Property for Data to visible >>

"When No Data "(Property of report) should be set accordingly.

Hope this might help some one....

Ranjit

 

 

ranjitsail's picture
Joined: May 14 2010 - 6:16am
Last seen: 8 years 10 months ago
0

I have done the same like above post,

 

In Subreport >>>>>>>>>>>>>>>>> put Following

very important >>> below address is field in my addressess List in Main Report

    <field name="address" class="java.lang.String"/>

 

and In Detail Section Put Following

            <textField isStretchWithOverflow="true" isBlankWhenNull="true">
                <reportElement x="142" y="20" width="92" height="20"/>
                <textElement>
                    <font size="12"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$F{address}]]></textFieldExpression>
            </textField>

But F{address} is expecting from  main JavaBean not from sub JavaBean.

Caused by: java.lang.NoSuchMethodException: Unknown property 'SubVoFeild1' on class 'class model.MainVo'.

I found there is a bug which is still open on the same issue

http://jasperforge.org/projects/ireport/tracker/view.php?id=4480

Please guide me any alternative solution for this issue.

Thank you,

Sowjanya

sowjanyamekala's picture
Joined: Jul 14 2010 - 11:39pm
Last seen: 8 years 8 months ago
1

Experts,

 I read this thread and I thought you may help me to resolve the similar issue.

I am trying to use multiple(2) subreports in one master report.  In subreport1 I am showing some database results and from subreport2(i want to display 3 columns of data called formula, status, result) . I am using empty datasource for the subreport3. Now, my question is, "can we pass objects to the reports from subreports?" I might have different values to display in the report via my java program. I have defined 3 parameters in the subreport2 called $P{formula},$ P{value} and $P{result}. It would not be feasible to use multiple parameters for same columns/data. I would like to know, can we pass objects via any mechanism? Can we store any mechanism which can store objects those eventually consist of different parameters? I searched on the forum and found JRBeanCollectionDataSource. Will it be useful for me? If yes would you please let me know step by step execution/changes required in the main report or subreports? I am not sure how to use this myBean thing. I would like to learn though. Is it the right direction to get for what I am looking for? Please suggest your ideas. Thank you in advance.

itpragmatic's picture
Joined: Aug 31 2010 - 3:24am
Last seen: 8 years 6 months ago
Feedback
randomness