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

passing parameters to subreport


jeysman

Recommended Posts

I'm having trouble passing a parameter to a subreport to perform a query. I've added the parameter to the subreport properties page in the report, and created the same parameter in the subreport, but it's not getting set properly.

 

Report:

<?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="Summary Report" pageWidth="595" pageHeight="842" columnWidth="535" leftMargin="30" rightMargin="30" topMargin="20" bottomMargin="20">
    <property name="ireport.scriptlethandling" value="0"/>
    <property name="ireport.encoding" value="UTF-8"/>
    <import value="net.sf.jasperreports.engine.*"/>
    <import value="java.util.*"/>
    <import value="net.sf.jasperreports.engine.data.*"/>
    <parameter name="Client_Name" class="java.lang.String"/>
    <parameter name="StartDate" class="java.util.Date">
        <defaultValueExpression><![CDATA[new SimpleDateFormat("yyyy-MM-dd").parse("2009-01-01")]]></defaultValueExpression>
    </parameter>
    <parameter name="EndDate" class="java.util.Date">
        <defaultValueExpression><![CDATA[new java.util.Date(System.currentTimeMillis()+((long)1000.0 * 60 * 60 * 24))]]></defaultValueExpression>
    </parameter>
    <parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA["C:\Documents and Settings\John\Desktop\"]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[sELECT
     client.`id` AS client_id,
     image.`id` AS image_id,
     image.`file_name` AS image_file_name,
     image.`time_to_rate` AS image_time_to_rate,
     image.`create_tms` AS image_create_tms,
     image_tag.`tag_id`,
     tag.`name`,
     client.`name` AS client_name
FROM
     `image` image INNER JOIN `client` client ON image.`client_id` = client.`id`
     LEFT OUTER JOIN `image_tag` image_tag ON image.`id` = image_tag.`image_id`
     LEFT OUTER JOIN `tag` tag ON image_tag.`tag_id` = tag.`id`
WHERE
     client.`name` = $P{Client_Name}]]>
    </queryString>
    <field name="client_id" class="java.lang.Integer"/>
    <field name="image_id" class="java.lang.Integer"/>
    <field name="image_file_name" class="java.lang.String"/>
    <field name="image_time_to_rate" class="java.lang.Integer"/>
    <field name="image_create_tms" class="java.sql.Timestamp"/>
    <field name="tag_id" class="java.lang.Integer"/>
    <field name="name" class="java.lang.String"/>
    <field name="client_name" class="java.lang.String"/>
    <variable name="TotalImagesRated" class="java.lang.Integer" calculation="Count">
        <variableExpression><![CDATA[$F{image_id}]]></variableExpression>
    </variable>
    <variable name="AverageTime" class="java.lang.Integer" calculation="Average">
        <variableExpression><![CDATA[$F{image_time_to_rate}]]></variableExpression>
    </variable>
    <variable name="TotalTime" class="java.lang.Integer" calculation="Sum">
        <variableExpression><![CDATA[$F{image_time_to_rate}]]></variableExpression>
    </variable>
    <variable name="ImagesByTag" class="java.lang.Integer" calculation="Count">
        <variableExpression><![CDATA[$F{image_id}]]></variableExpression>
    </variable>
    <group name="Client_ID">
        <groupExpression><![CDATA[$F{client_id}]]></groupExpression>
        <groupHeader>
            <band height="50">
                <textField isBlankWhenNull="false">
                    <reportElement key="textField" x="131" y="13" width="100" height="18"/>
                    <textElement/>
                    <textFieldExpression class="java.lang.String"><![CDATA[$F{client_name}]]></textFieldExpression>
                </textField>
                <staticText>
                    <reportElement key="staticText-6" x="25" y="11" width="88" height="20"/>
                    <textElement>
                        <font fontName="Cambria" size="14"/>
                    </textElement>
                    <text><![CDATA[Client:]]></text>
                </staticText>
                <line>
                    <reportElement key="line-1" x="25" y="38" width="324" height="1"/>
                </line>
            </band>
        </groupHeader>
        <groupFooter>
            <band height="109">
                <textField isBlankWhenNull="false">
                    <reportElement key="textField" x="243" y="6" width="100" height="18"/>
                    <textElement/>
                    <textFieldExpression class="java.lang.Integer"><![CDATA[$V{TotalImagesRated}]]></textFieldExpression>
                </textField>
                <staticText>
                    <reportElement key="staticText-2" x="49" y="6" width="146" height="18"/>
                    <textElement>
                        <font fontName="Cambria" size="14"/>
                    </textElement>
                    <text><![CDATA[Total Images Rated]]></text>
                </staticText>
                <staticText>
                    <reportElement key="staticText-3" x="49" y="31" width="182" height="17"/>
                    <textElement>
                        <font fontName="Cambria" size="14"/>
                    </textElement>
                    <text><![CDATA[Average Processing Time]]></text>
                </staticText>
                <textField isBlankWhenNull="false">
                    <reportElement key="textField" x="243" y="32" width="100" height="18"/>
                    <textElement/>
                    <textFieldExpression class="java.lang.Integer"><![CDATA[$V{AverageTime}]]></textFieldExpression>
                </textField>
                <staticText>
                    <reportElement key="staticText-4" x="49" y="57" width="182" height="18"/>
                    <textElement>
                        <font fontName="Cambria" size="14"/>
                    </textElement>
                    <text><![CDATA[Total Processing Time]]></text>
                </staticText>
                <textField isBlankWhenNull="false">
                    <reportElement key="textField" x="243" y="57" width="100" height="18"/>
                    <textElement/>
                    <textFieldExpression class="java.lang.Integer"><![CDATA[$V{TotalTime}]]></textFieldExpression>
                </textField>
                <subreport isUsingCache="true">
                    <reportElement key="subreport-1" x="55" y="79" width="167" height="19"/>
                    <subreportParameter name="Client_Name">
                        <subreportParameterExpression><![CDATA[$F{client_name}]]></subreportParameterExpression>
                    </subreportParameter>
                    <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                    <subreportExpression class="java.lang.String"><![CDATA[$P{SUBREPORT_DIR} + "Summary Report_subreport2.jasper"]]></subreportExpression>
                </subreport>
            </band>
        </groupFooter>
    </group>
    <background>
        <band/>
    </background>
    <title>
        <band height="7"/>
    </title>
    <pageHeader>
        <band height="50">
            <staticText>
                <reportElement key="staticText-5" x="104" y="7" width="320" height="36"/>
                <textElement textAlignment="Center">
                    <font size="24"/>
                </textElement>
                <text><![CDATA[usage Summary]]></text>
            </staticText>
        </band>
    </pageHeader>
    <columnHeader>
        <band height="30"/>
    </columnHeader>
    <detail>
        <band/>
    </detail>
    <columnFooter>
        <band/>
    </columnFooter>
    <pageFooter>
        <band height="9"/>
    </pageFooter>
    <lastPageFooter>
        <band height="50"/>
    </lastPageFooter>
    <summary>
        <band height="50"/>
    </summary>
    <noData>
        <band height="21"/>
    </noData>
</jasperReport>
 

 

SubReport:

<?xml version="1.0" encoding="UTF-8"  ?>
<!-- Created with iReport - A designer for JasperReports -->
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport
         name="Summary Report_subreport2"
         columnCount="1"
         printOrder="Vertical"
         orientation="Portrait"
         pageWidth="782"
         pageHeight="555"
         columnWidth="782"
         columnSpacing="0"
         leftMargin="0"
         rightMargin="0"
         topMargin="0"
         bottomMargin="0"
         whenNoDataType="NoPages"
         isTitleNewPage="false"
         isSummaryNewPage="false">
    <property name="ireport.scriptlethandling" value="0" />
    <property name="ireport.encoding" value="UTF-8" />
    <import value="java.util.*" />
    <import value="net.sf.jasperreports.engine.*" />
    <import value="net.sf.jasperreports.engine.data.*" />

    <parameter name="Client_Name" isForPrompting="false" class="java.lang.String">
        <defaultValueExpression ><![CDATA[$P{Client_Name}]]></defaultValueExpression>
    </parameter>
    <queryString><![CDATA[sELECT
     tag.`name`,
     count(image.`id`),
     client.`name` AS client_name
FROM
     `image` image INNER JOIN `image_tag` image_tag ON image.`id` = image_tag.`image_id`
     INNER JOIN `tag` tag ON image_tag.`tag_id` = tag.`id`
     INNER JOIN `client` client ON image.`client_id` = client.`id`
WHERE
     client.`name` = $P{Client_Name}
GROUP BY
     tag.`name`]]></queryString>

    <field name="name" class="java.lang.String"/>
    <field name="count(image.`id`)" class="java.lang.Long"/>
    <field name="client_name" class="java.lang.String"/>

        <background>
            <band height="0"  isSplitAllowed="true" >
            </band>
        </background>
        <title>
            <band height="0"  isSplitAllowed="true" >
            </band>
        </title>
        <pageHeader>
            <band height="0"  isSplitAllowed="true" >
            </band>
        </pageHeader>
        <columnHeader>
            <band height="18"  isSplitAllowed="true" >
                <rectangle>
                    <reportElement
                        mode="Opaque"
                        x="0"
                        y="0"
                        width="782"
                        height="17"
                        forecolor="#000000"
                        backcolor="#808080"
                        key="rectangle"/>
                    <graphicElement stretchType="NoStretch">
                    <pen lineWidth="0.0" lineStyle="Solid"/>
</graphicElement>
                </rectangle>
                <line direction="BottomUp">
                    <reportElement
                        x="0"
                        y="0"
                        width="782"
                        height="0"
                        forecolor="#000000"
                        backcolor="#FFFFFF"
                        key="line"/>
                    <graphicElement stretchType="NoStretch"/>
                </line>
                <line direction="BottomUp">
                    <reportElement
                        x="0"
                        y="15"
                        width="782"
                        height="0"
                        forecolor="#000000"
                        backcolor="#FFFFFF"
                        key="line"/>
                    <graphicElement stretchType="NoStretch"/>
                </line>
                <staticText>
                    <reportElement
                        x="0"
                        y="0"
                        width="121"
                        height="17"
                        forecolor="#FFFFFF"
                        key="staticText"/>
                    <box>                    <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.0" lineColor="#000000"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
</box>
                    <textElement>
                        <font size="12"/>
                    </textElement>
                <text><![CDATA[name]]></text>
                </staticText>
                <staticText>
                    <reportElement
                        x="121"
                        y="0"
                        width="254"
                        height="17"
                        forecolor="#FFFFFF"
                        key="staticText"/>
                    <box>                    <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.0" lineColor="#000000"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
</box>
                    <textElement>
                        <font size="12"/>
                    </textElement>
                <text><![CDATA[# of Images]]></text>
                </staticText>
            </band>
        </columnHeader>
        <detail>
            <band height="17"  isSplitAllowed="true" >
                <line direction="TopDown">
                    <reportElement
                        x="0"
                        y="16"
                        width="782"
                        height="0"
                        forecolor="#808080"
                        backcolor="#FFFFFF"
                        key="line"/>
                    <graphicElement stretchType="NoStretch"/>
                </line>
                <textField isStretchWithOverflow="false" pattern="" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None"  hyperlinkTarget="Self" >
                    <reportElement
                        x="0"
                        y="0"
                        width="121"
                        height="17"
                        forecolor="#000000"
                        key="textField"/>
                    <box>                    <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.0" lineColor="#000000"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
</box>
                    <textElement>
                        <font size="12" isBold="false" isItalic="false" isUnderline="false" isPdfEmbedded ="false" isStrikeThrough="false" />
                    </textElement>
                <textFieldExpression   class="java.lang.String"><![CDATA[$F{name}]]></textFieldExpression>
                </textField>
                <textField isStretchWithOverflow="false" pattern="" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None"  hyperlinkTarget="Self" >
                    <reportElement
                        x="121"
                        y="-1"
                        width="254"
                        height="17"
                        forecolor="#000000"
                        key="textField"/>
                    <box>                    <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.0" lineColor="#000000"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
</box>
                    <textElement>
                        <font size="12" isBold="false" isItalic="false" isUnderline="false" isPdfEmbedded ="false" isStrikeThrough="false" />
                    </textElement>
                <textFieldExpression   class="java.lang.Long"><![CDATA[$F{count(image.`id`)}]]></textFieldExpression>
                </textField>
            </band>
        </detail>
        <columnFooter>
            <band height="0"  isSplitAllowed="true" >
            </band>
        </columnFooter>
        <pageFooter>
            <band height="0"  isSplitAllowed="true" >
            </band>
        </pageFooter>
        <summary>
            <band height="0"  isSplitAllowed="true" >
            </band>
        </summary>
</jasperReport>
 

Link to comment
Share on other sites

  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

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