0
votes

I want to pass my parameter value from main report to the sub report.

I have tried many ways. Also I have tried options from this link as well passing-parameters-from-main-report-to-subreport-to-subreport-in-jasper

Main report

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.2.0.final using JasperReports Library version 6.2.0  -->
<!-- 2016-01-03T00:04:36 -->
<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="parademo" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="4949f116-97b5-453c-8581-61ccb1b3159e">
    <parameter name="TITLEPARA" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA["hi title"]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch">
            <textField>
                <reportElement x="180" y="36" width="220" height="30" uuid="4ac55979-2248-45d2-a001-3c82b5fec56b"/>
                <textFieldExpression><![CDATA[$P{TITLEPARA}]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="61" splitType="Stretch"/>
    </columnHeader>
    <detail>
        <band height="286" splitType="Stretch">
            <subreport runToBottom="false">
                <reportElement x="0" y="86" width="560" height="74" uuid="eca8556b-2d65-462f-a3d3-ac95ea6e3ebf"/>
                <subreportParameter name="TITLEPARA">
                    <subreportParameterExpression><![CDATA[$P{TITLEPARA}]]></subreportParameterExpression>
                </subreportParameter>
                <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                <subreportExpression><![CDATA["file:/home/rectrix/workspace/jasper/subparam.jrxml"]]></subreportExpression>
            </subreport>
        </band>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>

Sub-Report

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.2.0.final using JasperReports Library version 6.2.0  -->
<!-- 2016-01-03T00:04:17 -->
<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="subparam" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="b4c019e3-0dc2-425c-b449-908c4fd21bde">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="MainReportDataSet"/>
    <parameter name="TITLEPARA" class="java.lang.String" isForPrompting="false"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch">
            <textField>
                <reportElement x="140" y="45" width="100" height="30" uuid="6b9d5038-4163-47ed-a0cd-812f07b27ff0"/>
                <textFieldExpression><![CDATA[$P{TITLEPARA}]]></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>

I am able to print "HI TITLE" Using the parameter in my main report's title band.

I want to pass this value to the sub-report using the parameter and print it on the title band of the sub-report.

I want to pass this value using a parameter only, because I want to know how the parameters works so I could use it for my project to pass the values to sub-report through parameters.

1

1 Answers

2
votes

You are passing the parameter correctly, this is not your issue.

Using your example files you have these issues.

  1. If the datasource is empty you need attribute whenNoDataType="AllSectionsNoDetail" on jasperReport tag to display something (both in main and subreport).

  2. The subreport can not be in the detail band (since your datasource is empty), move it into another band example title band.

  3. You can not reference a jrxml in subreportExpression, you need to reference a .jasper file (the jrxml when it has been complied). Note: the path need's to be the absolute path to the file (you normally use a parameter for this so that you can pass it in runtime)

see this for more information on path to subreport Subreport not rendering in main report

For testing I suggest you to put another static text element in the subreport, so that you understand if the subreport is displayed or not.