Jump to content
We've recently updated our Privacy Statement, available here ×
  • How to pass variable value from sub dataset to main dataset


    prusyn
    • Features: Reports Version: v7.2 Product: Jaspersoft® Studio

    Issue:

    [toc on_off::hide=1]

    Customer has a report designed in Jaspersoft Studio, in which there is a table that uses its dataset (subdataset). For this subdataset customer has defined a variable. Now he wants to display the value of this variable in report itself, outside the table. Which means he needs to pass the variable value from sub dataset to main dataset. Which is impossible by default. Customer receives error "You drop an object from a subdataset into the report": http://prntscr.com/n2mezy


    Resolution:

    In order to pass variable value from sub dataset to main dataset, you have to define variable in main dataset with calculation System: 

     

    <variable name="Counter" class="java.lang.Integer" calculation="System"/>

     

    Now you should add returnValue property into datasetRun section like below. It takes fromVariable (the one from subdataset) and toVariable (the one from main dataset): 

     

    <datasetRun subDataset="Dataset1" uuid="b793d24c-3ae9-4ded-b083-ff57b900a2b8"> 
        <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression> 
        <returnValue fromVariable="Total Summary" toVariable="Counter"/> 
    </datasetRun> 

    See report attached.

    sampletable.jrxml


    User Feedback

    Recommended Comments

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- Created with Jaspersoft Studio version 6.12.2.final using JasperReports Library version 6.12.2-75c5e90a222ab406e416cbf590a5397028a52de3  -->
    <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="bidderAttachementReport_16June" pageWidth="915" pageHeight="842" columnWidth="875" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="13342f84-2ed2-4663-8b82-a764de319651">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="My Data Adapter "/>
        <property name="com.jaspersoft.studio.data.sql.tables" value=""/>
        <property name="ireport.jasperserver.url" value="http://localhost:8080/jasperserver/"/>
        <property name="ireport.jasperserver.user" value="jasperadmin"/>
        <property name="ireport.jasperserver.report.resource" value="/reports/My_Report_files/main_jrxml"/>
        <property name="ireport.jasperserver.reportUnit" value="/reports/My_Report"/>
        <style name="Table_TH" mode="Opaque" backcolor="#F0F8FF">
            <box>
                <pen lineWidth="0.5" lineColor="#000000"/>
                <topPen lineWidth="0.5" lineColor="#000000"/>
                <leftPen lineWidth="0.5" lineColor="#000000"/>
                <bottomPen lineWidth="0.5" lineColor="#000000"/>
                <rightPen lineWidth="0.5" lineColor="#000000"/>
            </box>
        </style>
        <style name="Table_CH" mode="Opaque" backcolor="#BFE1FF">
            <box>
                <pen lineWidth="0.5" lineColor="#000000"/>
                <topPen lineWidth="0.5" lineColor="#000000"/>
                <leftPen lineWidth="0.5" lineColor="#000000"/>
                <bottomPen lineWidth="0.5" lineColor="#000000"/>
                <rightPen lineWidth="0.5" lineColor="#000000"/>
            </box>
        </style>
        <style name="Table_TD" mode="Opaque" backcolor="#FFFFFF">
            <box>
                <pen lineWidth="0.5" lineColor="#000000"/>
                <topPen lineWidth="0.5" lineColor="#000000"/>
                <leftPen lineWidth="0.5" lineColor="#000000"/>
                <bottomPen lineWidth="0.5" lineColor="#000000"/>
                <rightPen lineWidth="0.5" lineColor="#000000"/>
            </box>
            <conditionalStyle>
                <conditionExpression><![CDATA[new Boolean($V{REPORT_COUNT}.intValue()%2==0)]]></conditionExpression>
                <style backcolor="#F0F8FF"/>
            </conditionalStyle>
        </style>
        <subDataset name="myDataset" uuid="83d46592-75d8-4953-a609-e82627909f3f">
            <property name="com.jaspersoft.studio.data.sql.tables" value=""/>
            <property name="com.jaspersoft.studio.data.defaultdataadapter" value="My Data Adapter "/>
            <property name="com.jaspersoft.studio.data.sql.SQLQueryDesigner.sash.w1" value="373"/>
            <property name="com.jaspersoft.studio.data.sql.SQLQueryDesigner.sash.w2" value="620"/>
            <parameter name="acId" class="java.util.List" nestedType="java.lang.Integer"/>
            <queryString language="SQL">
                <![CDATA[sELECT field1,field2,field3,field4 FROM MY_TBL]]>
            </queryString>
            <field name="field1" class="java.lang.Integer">
                <property name="com.jaspersoft.studio.field.label" value="field1"/>
                <property name="com.jaspersoft.studio.field.tree.path" value="fieldVal3"/>
            </field>
            <field name="field2" class="java.lang.String">
                <property name="com.jaspersoft.studio.field.label" value="field2"/>
                <property name="com.jaspersoft.studio.field.tree.path" value="fieldVal2"/>
            </field>
            <field name="field3" class="java.lang.String">
                <property name="com.jaspersoft.studio.field.label" value="field3"/>
                <property name="com.jaspersoft.studio.field.tree.path" value="fieldVal3"/>
            </field>
            <field name="field4" class="java.sql.Timestamp">
                <property name="com.jaspersoft.studio.field.label" value="field4"/>
                <property name="com.jaspersoft.studio.field.tree.path" value="fieldVal4"/>
            </field>

            <variable name="fromVarField3" class="java.lang.String" calculation="Sum">
                <variableExpression><![CDATA[$F{field3}]]></variableExpression>
            </variable>
        </subDataset>
        <parameter name="param1" class="java.util.Date"/>
        <parameter name="param2" class="java.util.Date"/>
        <parameter name="param3" class="java.lang.Integer"/>
        <parameter name="param4" class="java.lang.Integer" nestedType="java.lang.Integer"/>
        <parameter name="param5" class="java.util.List" nestedType="java.lang.Integer"/>
        <queryString>
            <![CDATA[sELECT * FROM my_table1]]>
        </queryString>
        <field name="tblFld" class="java.lang.String">
            <property name="com.jaspersoft.studio.field.label" value="tblFld"/>
            <property name="com.jaspersoft.studio.field.tree.path" value="tblFldVal"/>
        </field>
        <variable name="toVarField3" class="java.lang.String" calculation="System"/>
        <background>
            <band splitType="Stretch"/>
        </background>
        <title>
            <band height="79" splitType="Stretch">
                <staticText>
                    <reportElement x="230" y="20" width="259" height="30" forecolor="#074263" uuid="9324ad35-65ff-4098-800d-77aebeb3c47d"/>
                    <textElement textAlignment="Center">
                        <font size="16" isBold="true"/>
                    </textElement>
                    <text><![CDATA[MY REPORT]]></text>
                </staticText>
            </band>
        </title>
        <detail>
            <band height="162" splitType="Stretch">
                <componentElement>
                    <reportElement x="-10" y="70" width="895" height="60" uuid="af601335-3deb-4640-af93-36b870d193e3">
                        <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                        <property name="com.jaspersoft.studio.table.style.table_header" value="Table_TH"/>
                        <property name="com.jaspersoft.studio.table.style.column_header" value="Table_CH"/>
                        <property name="com.jaspersoft.studio.table.style.detail" value="Table_TD"/>
                        <property name="com.jaspersoft.studio.components.autoresize.proportional" value="true"/>
                    </reportElement>
                    <jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
                        <datasetRun subDataset="myDataset" uuid="21656787-5bfb-48e2-b40f-e6c7198a1fb4">
                            <datasetParameter name="myId">
                                <datasetParameterExpression><![CDATA[$P{param5}]]></datasetParameterExpression>
                            </datasetParameter>
                            <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                            <returnValue fromVariable="fromVarField3" toVariable="toVarField3"/>
                        </datasetRun>
                        <jr:column width="68" uuid="acc6a027-54fe-41f7-8848-cd1bada476d0">
                            <jr:columnHeader style="Table_CH" height="30">
                                <staticText>
                                    <reportElement x="0" y="0" width="68" height="30" uuid="75c8d2f1-290c-4b48-880e-7801a7a21943"/>
                                    <text><![CDATA[field1]]></text>
                                </staticText>
                            </jr:columnHeader>
                            <jr:detailCell style="Table_TD" height="30">
                                <textField>
                                    <reportElement x="0" y="0" width="68" height="30" uuid="7b534f76-0878-4903-85f8-aaa6190a088d"/>
                                    <textFieldExpression><![CDATA[$F{field1}]]></textFieldExpression>
                                </textField>
                            </jr:detailCell>
                        </jr:column>
                        <jr:column width="68" uuid="b9a4a0a8-a49b-44dd-bc72-8b7fa8dd1131">
                            <jr:columnHeader style="Table_CH" height="30">
                                <staticText>
                                    <reportElement x="0" y="0" width="68" height="30" uuid="50ae0b8b-def1-4660-af20-cad53f7747e2"/>
                                    <text><![CDATA[field2]]></text>
                                </staticText>
                            </jr:columnHeader>
                            <jr:detailCell style="Table_TD" height="30">
                                <textField>
                                    <reportElement x="0" y="0" width="68" height="30" uuid="d22054da-45c4-4b5f-8e66-72064db5c465"/>
                                    <textFieldExpression><![CDATA[$F{field2}]]></textFieldExpression>
                                </textField>
                            </jr:detailCell>
                        </jr:column>
                        <jr:column width="68" uuid="d4a132f6-290c-45b8-8c6f-1d7e83318591">
                            <jr:columnHeader style="Table_CH" height="30">
                                <staticText>
                                    <reportElement x="0" y="0" width="68" height="30" uuid="9bbc631f-f353-47c6-b9b8-6f873e7bf171"/>
                                    <text><![CDATA[field4]]></text>
                                </staticText>
                            </jr:columnHeader>
                            <jr:detailCell style="Table_TD" height="30">
                                <textField>
                                    <reportElement x="0" y="0" width="68" height="30" uuid="4d02ffe3-558c-4f0e-9004-7333a6fbf7c8"/>
                                    <textFieldExpression><![CDATA[$F{field4}]]></textFieldExpression>
                                </textField>
                            </jr:detailCell>
                        </jr:column>
                    </jr:table>
                </componentElement>
                <staticText>
                    <reportElement x="180" y="10" width="90" height="20" uuid="cff9134d-7c93-4d69-b4b0-d83b6cf8e1a0"/>
                    <textElement textAlignment="Center">
                        <font size="10"/>
                    </textElement>
                    <text><![CDATA[PassedField]]></text>
                </staticText>
                <textField evaluationTime="Report">
                    <reportElement x="280" y="10" width="100" height="20" uuid="95af0348-ba6f-4a34-bc07-36ac5305ec23"/>
                    <textElement markup="rtf"/>
                    <textFieldExpression><![CDATA[$V{toVarField3}]]></textFieldExpression>
                </textField>
            </band>
        </detail>
    </jasperReport>
     

     

     

     

    PLEASE COPY & PASTE ABOVE XML IN A EDITOR

    Link to comment
    Share on other sites

    Your  fromVarField3 variable is type of String, but is set to  calculation="Sum". Why? What is the logic behind that?

    With its current implementation it will not work. Can you please download the sample attached to the article and check it?

    Link to comment
    Share on other sites

    For more context, the exact value of fromVarField3 has to be printed somewhere in the main report.

    I havn't found any option useful for the same among Count, Sum, Average, Lowest, Highest, Standard Deviation, Variance, System, First, Distinct Count so I chose your option, i.e. Sum.

    Kindly tell what should be used.

    Link to comment
    Share on other sites

    If you want to use calculations, you have to use Numbers, at very least variable type should be Integer, not String, cause SUM function returns Number value.

    Link to comment
    Share on other sites

    Thank you prusyn for your update.

     

    I thought your solution will work for all kinds of data, i.e. number, text etc.

    For my case the data which should be passed is string which is not supported according to your comment.

    For number it is great - I have compiled and executed your jrxml.

    Link to comment
    Share on other sites

    I think you have got my point very clearly. The use case I am still struggling is specifically to pass the value of a string type field (fixed for all records) of a dataset inside a table to the main report i.e. outside of this table.

    In this scenario, what eaxctly need to be done instead of Calculation that doesn't work on Strings and only works on Numbers - please let me know as I am not aware of the steps.

     

    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...