Jump to content
We've recently updated our Privacy Statement, available here ×

[Solved] Problem passing datasource to subreport


mahoushi

Recommended Posts

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
Link to comment
Share on other sites

  • Replies 9
  • Created
  • Last Reply

Top Posters In This Topic

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?

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

  • 1 month later...

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

Link to comment
Share on other sites

  • 2 months later...

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.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...