Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning a value from Sub Report to Main Report in iReport

Hi every one I am using iReports for generating one of the reports and stuck at one place.

The situation is like this:

I am using one sub report in my main report and i want to return a variable (float) back to the main report from sub report after the query has been executed. And i'm just getting null values back to main report i have wasted 2 days googling and searching but problem is still there..

bellow is my dummy code of my JRXMLS (perfectly same) and snaps...

Main Report JRXML

  <?xml version="1.0" encoding="UTF-8"?>
    <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="anuj" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
        <property name="ireport.zoom" value="1.0"/>
        <property name="ireport.x" value="0"/>
        <property name="ireport.y" value="0"/>
        <parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
            <defaultValueExpression><![CDATA["/home/anuj/Reports/"]]></defaultValueExpression>
        </parameter>
        <queryString language="SQL">
            <![CDATA[select * from "SensorType"]]>
        </queryString>
        <field name="SensorTypeId" class="java.lang.Integer"/>
        <field name="SensorTypeName" class="java.lang.String"/>
        <variable name="A" class="java.lang.Integer" resetType="None" calculation="System"/>
        <background>
            <band splitType="Stretch"/>
        </background>
        <title>
            <band splitType="Stretch"/>
        </title>
        <pageHeader>
            <band splitType="Stretch"/>
        </pageHeader>
        <columnHeader>
            <band splitType="Stretch"/>
        </columnHeader>
        <detail>
            <band height="23" splitType="Stretch">
                <textField>
                    <reportElement x="71" y="3" width="100" height="20"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[$F{SensorTypeId}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="202" y="3" width="112" height="20"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[$F{SensorTypeName}]]></textFieldExpression>
                </textField>
            </band>
        </detail>
        <columnFooter>
            <band height="5" splitType="Stretch"/>
        </columnFooter>
        <pageFooter>
            <band height="1" splitType="Stretch"/>
        </pageFooter>
        <summary>
            <band height="42" splitType="Stretch">
                <subreport>
                    <reportElement x="183" y="16" width="257" height="26"/>
                    <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                    <returnValue subreportVariable="A" toVariable="A"/>
                    <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "anuj_subreport1.jasper"]]></subreportExpression>
                </subreport>
                <textField>
                    <reportElement x="71" y="22" width="100" height="20"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[$V{A}]]></textFieldExpression>
                </textField>
            </band>
        </summary>
    </jasperReport>

Sub Report JRXML

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="anuj_subreport1" language="groovy" pageWidth="555" pageHeight="802" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="149"/>
    <queryString>
        <![CDATA[select Sum("SensorTypeId") from "SensorType";]]>
    </queryString>
    <field name="sum" class="java.lang.Long"/>
    <variable name="A" class="java.lang.Integer" resetType="None" calculation="System">
        <variableExpression><![CDATA[$F{sum}]]></variableExpression>
    </variable>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch">
            <staticText>
                <reportElement x="169" y="59" width="100" height="20"/>
                <textElement/>
                <text><![CDATA[sum]]></text>
            </staticText>
            <textField>
                <reportElement x="216" y="59" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{sum}]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="61" splitType="Stretch"/>
    </columnHeader>
    <detail>
        <band height="125" splitType="Stretch"/>
    </detail>
        <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>

Thanks in advance..

like image 448
Anuj Patel Avatar asked Sep 08 '11 12:09

Anuj Patel


1 Answers

Although the question is already answered, I would like to highlight the importance of having calculation="System" on the variable in the Main Report (the report calling the SubReport).

I wasted a lot of time before figuring this out...

In the dummy-code for Main Report JRXML above, the variable "A" correctly has calculation="System". NB: The answer by @GenericJon covers the variable in the SubReport.

like image 156
bubba_hego99 Avatar answered Feb 23 '23 19:02

bubba_hego99