How to evaluate a jasper report variable when there is no data? - jasper-reports

I have a report that using a parameter to determine the report title. It is displaying null when there is no data. I've tried changing the variable $V{reportName} Evaluation Time, but no luck. Can anybody help me?
Thanks.
PS: It is using mssql data adapter and the current querystring does not return any row. To return rows, change 2=1 to 1=1.
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 5.6.1.final using JasperReports Library version 5.6.1 -->
<!-- 2017-05-02T07:59:20 -->
<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="Evaluate" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="a736ce53-7e76-4194-9086-96d2270a6250">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="SYDPC1744-EXPRESSR2"/>
<parameter name="ReportType" class="java.lang.String">
<defaultValueExpression><![CDATA["B"]]></defaultValueExpression>
</parameter>
<queryString language="SQL">
<![CDATA[select Col1 from (values (1),(2)) as a(Col1) where 2=1]]>
</queryString>
<field name="Col1" class="java.lang.Integer"/>
<variable name="ReportName" class="java.lang.String">
<variableExpression><![CDATA[$P{ReportType}.equals("A")? $V{reportTitleA}:$V{reportTitleB}]]></variableExpression>
</variable>
<variable name="reportTitleA" class="java.lang.String">
<variableExpression><![CDATA["Report A"]]></variableExpression>
</variable>
<variable name="reportTitleB" class="java.lang.String">
<variableExpression><![CDATA["Report Other"]]></variableExpression>
</variable>
<pageHeader>
<band height="49" splitType="Stretch">
<textField evaluationTime="Report">
<reportElement x="0" y="0" width="390" height="20" uuid="98c519b9-7350-4cd6-a71e-f2fb1c0e7b80"/>
<textFieldExpression><![CDATA[$V{ReportName}]]></textFieldExpression>
</textField>
</band>
</pageHeader>
<columnHeader>
<band height="14" splitType="Stretch"/>
</columnHeader>
<detail>
<band height="15" splitType="Stretch">
<property name="local_mesure_unitheight" value="pixel"/>
<property name="com.jaspersoft.studio.unit.height" value="px"/>
<textField>
<reportElement x="0" y="0" width="100" height="14" uuid="82ab5741-1138-4173-88f5-42ef07a6cb9f">
<property name="local_mesure_unitheight" value="pixel"/>
<property name="com.jaspersoft.studio.unit.height" value="px"/>
</reportElement>
<textFieldExpression><![CDATA[$F{Col1}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>

Your problem was setting the <textFieldExpression>
and not the <initialValueExpression>.
The <textFieldExpression> is used for calculation. Only use it for calculation!
The <initialValueExpression> is used for your initial value of this variable.
When there is no data jasperreports won't go through the calculation expression as there is no way, you could calculate using no data at all.
So just use the initial value expression...
Chaning the evaluation time of the element has no benefits at all in this case, so you can just skip it.
Also keep in mind, that by using a good order for your variables you can avoid many errors.
Here is the working code (excerpt):
<variable name="reportTitleA" class="java.lang.String">
<initialValueExpression><![CDATA["Report A"]]></initialValueExpression>
</variable>
<variable name="reportTitleB" class="java.lang.String">
<initialValueExpression><![CDATA["Report Other"]]></initialValueExpression>
</variable>
<variable name="ReportName" class="java.lang.String">
<initialValueExpression><![CDATA[$P{ReportType}.equals("A")? $V{reportTitleA}:$V{reportTitleB}]]></initialValueExpression>
</variable>
<pageHeader>
<band height="49" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="390" height="20" uuid="98c519b9-7350-4cd6-a71e-f2fb1c0e7b80"/>
<textFieldExpression><![CDATA[$V{ReportName}]]></textFieldExpression>
</textField>
</band>
</pageHeader>

Related

Jasper Json nested lists

I'm new in Jasper reports, so I'll write my whole task, if it's allowed..
I'm trying to parse json data to print in it PDF. Data is coming from oracle DB, written in CLOB column, so I have list of json-clob records.
I have nested lists json (and clob) data, so I can't build 3 stair nested report with my jasper experience. I have json structure like this:
{
title: 'Main Title',
blocks: [{
title: 'Inner Title',
items: [{
key: '1',
value: 'some text'
}, {
key: '2',
value: 'some other text'
}]
}, {
text: 'here may be other fields (text, not title)'
}]
}
For this json data, I want to get this result in PDF (just simple output):
Main Title
Inner Title
1 some text
2 some other text
here may be other fields (text, not title)
Firstly, I use CLOB converting in json source ( script:
< dataSourceExpression >
< ![CDATA[new net.sf.jasperreports.engine.data.JsonQLDataSource(new ByteArrayInputStream($F{JSON_CLOB_RECORD}.getBytes("UTF-8")))]] >
< /dataSourceExpression >
source: nested jasper subreports with json datasource ).
After that, I use jr:list and textfield to print main titles. And I'm done, don't know how to continue.
I searched nested lists (jr:list) but nothing, can't fit to my task.
P.S. I have subDataset for parsing data from CLOB to json with fields title (java.lang.String) and blocks (I tried here java.util.Collection and ArrayList but errors occurred, than I wrote java.lang.Object. Is it necessary to write blocks field in this subDataset?). I also added subDataset for blocks list, is it needed? And if yes, so I have to add items subDataset too, right? Also, I tried to add subReport after main title textfield, for blocks list and added dataSourceExpression but can't continue.
Need some help.
I would suggest using a subreport, instead of a list or nested lists, because you gain more flexibility in this case.
In the main report you should discard the list element and the dataset and use a subreport, something like:
<subreport>
<reportElement x="0" y="0" width="550" height="50" uuid="37861aca-d444-4aec-ad03-baa310540327"/>
<subreportParameter name="JSON_INPUT_STREAM">
<subreportParameterExpression><![CDATA[new ByteArrayInputStream($F{JSON_CLOB_RECORD}.getBytes("UTF-8"))]]></subreportParameterExpression>
</subreportParameter>
<subreportExpression><![CDATA["Subreport.jasper"]]></subreportExpression>
</subreport>
Here we only need the JSON_INPUT_STREAM so that the subreport will construct a dataSource based on the query and its language.
Case 1. If you are mostly interested in the items key, then the subreport(Subreport.jrxml) could be pretty basic like so:
<?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="Subreport" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="a3dd10f8-b28c-4c11-b582-9b169e8aa417">
<queryString language="jsonql">
<![CDATA[..items.*]]>
</queryString>
<field name="mainTitle" class="java.lang.String">
<property name="net.sf.jasperreports.jsonql.field.expression" value="$.title"/>
</field>
<field name="key" class="java.lang.String">
<property name="net.sf.jasperreports.jsonql.field.expression" value="key"/>
</field>
<field name="value" class="java.lang.String">
<property name="net.sf.jasperreports.jsonql.field.expression" value="value"/>
</field>
<field name="blockTitle" class="java.lang.String">
<property name="net.sf.jasperreports.jsonql.field.expression" value="^^.title"/>
</field>
<background>
<band splitType="Stretch"/>
</background>
<detail>
<band height="90" splitType="Stretch">
<textField>
<reportElement isPrintRepeatedValues="false" x="0" y="30" width="100" height="30" isRemoveLineWhenBlank="true" uuid="172de051-3d2c-4458-b01e-ab06e4e1bb3b"/>
<textFieldExpression><![CDATA[$F{blockTitle}]]></textFieldExpression>
</textField>
<textField>
<reportElement isPrintRepeatedValues="false" x="0" y="0" width="100" height="30" isRemoveLineWhenBlank="true" uuid="94f57756-a5a8-402f-ad23-7cdbe2a00e59"/>
<textFieldExpression><![CDATA[$F{mainTitle}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="0" y="60" width="100" height="30" uuid="7dbbdfd8-5a0e-4288-ae39-21f13cd4d921"/>
<textFieldExpression><![CDATA[$F{key} + " " + $F{value}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
Notice the isPrintRepeatedValues="false" and the isRemoveLineWhenBlank="true" properties that remove the duplicates and the lines so that you get the desired output.
I've simulated your case with a serialized JSON and got the following:
Case 2. If you are interested in all the blocks object data you would have to land on the blocks item in your query and create a list/subreport for the items key, something like:
<?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="Report" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="a3dd10f8-b28c-4c11-b582-9b169e8aa417">
<subDataset name="BlockItemsDataset" uuid="83c1b8ed-e880-4474-a809-39d95277341f">
<field name="key" class="java.lang.String">
<property name="net.sf.jasperreports.jsonql.field.expression" value="key"/>
</field>
<field name="value" class="java.lang.String">
<property name="net.sf.jasperreports.jsonql.field.expression" value="value"/>
</field>
</subDataset>
<queryString language="jsonql">
<![CDATA[blocks]]>
</queryString>
<field name="mainTitle" class="java.lang.String">
<property name="net.sf.jasperreports.jsonql.field.expression" value="$.title"/>
</field>
<field name="blockTitle" class="java.lang.String">
<property name="net.sf.jasperreports.jsonql.field.expression" value="title"/>
</field>
<background>
<band splitType="Stretch"/>
</background>
<detail>
<band height="91" splitType="Stretch">
<textField>
<reportElement isPrintRepeatedValues="false" x="0" y="0" width="100" height="30" isRemoveLineWhenBlank="true" uuid="94f57756-a5a8-402f-ad23-7cdbe2a00e59"/>
<textFieldExpression><![CDATA[$F{mainTitle}]]></textFieldExpression>
</textField>
<textField>
<reportElement isPrintRepeatedValues="false" x="0" y="30" width="100" height="30" isRemoveLineWhenBlank="true" uuid="172de051-3d2c-4458-b01e-ab06e4e1bb3b"/>
<textFieldExpression><![CDATA[$F{blockTitle}]]></textFieldExpression>
</textField>
<componentElement>
<reportElement x="0" y="61" width="200" height="30" uuid="7a94d84d-d58e-4508-b143-3704fa7f5cd3"/>
<jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
<datasetRun subDataset="BlockItemsDataset" uuid="a866f955-7594-4fb9-9bb2-936282963dea">
<dataSourceExpression><![CDATA[((net.sf.jasperreports.engine.data.JsonQLDataSource)$P{REPORT_DATA_SOURCE}).subDataSource("items")]]></dataSourceExpression>
</datasetRun>
<jr:listContents height="30" width="200">
<textField>
<reportElement x="0" y="0" width="200" height="30" uuid="36a771fa-f949-4c69-a62c-1df0c610a2d3"/>
<textFieldExpression><![CDATA[$F{key} + " " + $F{value}]]></textFieldExpression>
</textField>
</jr:listContents>
</jr:list>
</componentElement>
</band>
</detail>
</jasperReport>

How to pass datetime parameter to postgresql sql?

I am using Jaspersoft studio to create a report. The data adapter is a postgresql query (to a postgres server). The query:
select * from dbo.FACTSALES where FactSales.saledatekey BETWEEN to_char($P!{StartDate}, 'YYYYMMDD')::INTEGER AND to_char($P!{EndDate}, 'YYYYMMDD')::INTEGER
FactSales.saledatekey is Integer.
My plan is to put two data time parameters (StartDate, EndDate) in the report so I can choose start and end dates to run the report. So the query is as above and I also created two parameters in Outline with the same names and of type java.sql.Timestamp (no default expression). But When i run the report I get Error executing SQL Statement.
EDIT
so I changed $P! to $P
select * from dbo.FACTSALES where FactSales.saledatekey BETWEEN to_char($P{StartDate}, 'YYYYMMDD')::INTEGER AND to_char($P{EndDate}, 'YYYYMMDD')::INTEGER
But I still get the same error.
jrxml (I changed from select * to just two columns):
<?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="test4" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="fd68b751-49c2-4153-b2a8-48a95af021c9">
<property name="com.jaspersoft.studio.data.sql.tables" value=""/>
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="datamart"/>
<property name="ireport.jasperserver.url" value="http://10.20.169.43:8080/jasperserver/"/>
<property name="ireport.jasperserver.user" value="Nian"/>
<property name="ireport.jasperserver.report.resource" value="/reports/NianTest/test4_files/main_jrxml"/>
<property name="ireport.jasperserver.reportUnit" value="/reports/NianTest/test4"/>
<parameter name="StartDate" class="java.sql.Timestamp">
<parameterDescription><![CDATA[]]></parameterDescription>
</parameter>
<parameter name="EndDate" class="java.sql.Timestamp"/>
<queryString>
<![CDATA[select customerkey, productkey from dbo.FACTSALES where $X{[BETWEEN], FactSales.saledatekey , to_char($P{StartDate} , 'YYYYMMDD')::INTEGER, to_char($P{EndDate}, 'YYYYMMDD')::INTEGER}]]>
</queryString>
<field name="customerkey" class="java.lang.Integer"/>
<field name="productkey" class="java.lang.Integer"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch">
<staticText>
<reportElement x="50" y="0" width="100" height="30" uuid="1471918d-b246-4aea-ba5c-d65a7fa48284"/>
<text><![CDATA[customerkey]]></text>
</staticText>
<staticText>
<reportElement x="268" y="0" width="100" height="30" uuid="361e8408-7859-483f-8143-5834fff3594d"/>
<text><![CDATA[productkey]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="125" splitType="Stretch">
<textField>
<reportElement x="50" y="49" width="100" height="30" uuid="3423ea0c-aa51-4537-ab28-29ae255c313c"/>
<textFieldExpression><![CDATA[$F{customerkey}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="200" y="40" width="100" height="30" uuid="d8381278-1fc1-4f9f-bd85-e65a85b77630"/>
<textFieldExpression><![CDATA[$F{productkey}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
and two parameters:
StartDate class: java.sql.Timestamp
EndDate class: java.sql.Timestamp
The error:
ERROR: function to_char(unknown, unknown) is not unique Hint: Could not choose a best candidate function. You might need to add explicit type casts
The query using prepared statement will be:
select customerkey, productkey from dbo.FACTSALES where FactSales.saledatekey BETWEEN to_char($P{StartDate}::timestamp, 'YYYYMMDD')::int AND to_char($P{EndDate}::timestamp, 'YYYYMMDD')::int
or change you parameter to Integer and use the build-in between function
select customerkey, productkey from dbo.FACTSALES where $X{[BETWEEN], FactSales.saledatekey ,StartDate, EndDate}
If you like to test in IDE always consider to set default values to parameters.
<parameter name="StartDate" class="java.sql.Timestamp" isForPrompting="false">
<defaultValueExpression><![CDATA[new java.sql.Timestamp(1l)]]></defaultValueExpression>
</parameter>
<parameter name="EndDate" class="java.sql.Timestamp" isForPrompting="false">
<defaultValueExpression><![CDATA[new java.sql.Timestamp(new java.util.Date().getTime())]]></defaultValueExpression>
</parameter>

display column header in jasper reports

Trying to create a jasper report, I can see the data when I export to xls or csv. But column header is missing, not sure whats wrong with it, following is my report. Thanks in Advance.
<?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="scireport" language="groovy" printOrder="Horizontal" pageWidth="5000" pageHeight="792" whenNoDataType="AllSectionsNoDetail" columnWidth="4960" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" isIgnorePagination="true">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<queryString language="SQL">
<![CDATA[select PICKUP_DATE from SCI_PLDDATA_VIEW order by EVENT_TIMESTAMP DESC]]>
</queryString>
<field name="PICKUP_DATE" class="java.sql.Timestamp">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<group name="stopid">
<groupHeader>
<band height="50"/>
</groupHeader>
</group>
<columnHeader>
<band height="44" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="900" height="20"/>
<textElement>
<font size="14" isBold="true"/>
</textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$R{jasper.PICKUP_DATE}]]></textFieldExpression>
</textField>
</band>
</columnHeader>
<detail>
<band height="34" splitType="Stretch">
<textField isStretchWithOverflow="true" pattern="MM-dd-yyyy HH:mm" isBlankWhenNull="true">
<reportElement x="0" y="0" width="900" height="20"/>
<textElement/>
<textFieldExpression class="java.sql.Timestamp"><![CDATA[$F{PICKUP_DATE}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
You are fetching data from database , your value will come in field. You should use $F{} instead of $R{} in column header.

Dynamicaly pass data fro a java object to jasper report and convert to PDF

I am new in Jasper/ I report. I am trying to create a report that access or gets its data from the java class. I have been trying to use JRBeanDataSource with no luck. I want the report to be dynamic in code without using i-report. I tried designing a report from i report but i cant seem to make the report compile. here is the error
 Compiling to file... E:\Program Files (x86)\Jaspersoft\iReport-5.0.1\ireport\projects\report1.jasper
net.sf.jasperreports.engine.design.JRValidationException: Report design not valid :       1. Parameter not found : name      2. Parameter not found : surname     at net.sf.jasperreports.engine.design.JRAbstractCompiler.verifyDesign(JRAbstractCompiler.java:271)     at net.sf.jasperreports.engine.design.JRAbstractCompiler.compileReport(JRAbstractCompiler.java:153)     at net.sf.jasperreports.engine.JasperCompileManager.compile(JasperCompileManager.java:240)     at net.sf.jasperreports.engine.JasperCompileManager.compileToFile(JasperCompileManager.java:156)     at net.sf.jasperreports.engine.JasperCompileManager.compileReportToFile(JasperCompileManager.java:439)     at com.jaspersoft.ireport.designer.compiler.IReportCompiler.run(IReportCompiler.java:528)     at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:572)     at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:997)
Compilation running time: 60!
The report XML is
<?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="report1" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" whenResourceMissingType="Empty" uuid="a686c2f8-36e1-48f1-8894-8324c9f5d041">
<property name="ireport.zoom" value="1.4641000000000006"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="24"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch"/>
</columnHeader>
<detail>
<band height="125" splitType="Stretch">
<textField>
<reportElement uuid="b1370c0a-6fd1-44ec-af9a-f4edba088c48" x="97" y="21" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$P{name}]]></textFieldExpression>
</textField>
<textField>
<reportElement uuid="06c14f56-9416-4d14-a6d3-7f5dbfa6d701" x="97" y="59" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$P{surname}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
can some one help with this?
You refer in the report to 2 parameter expressions: $P{name}, $P{surname}.
These have not been defined.
In order to make your report executable you'd need to add definitions for these parameters, e.g.
..
<property name="ireport.y" value="24"/>
<parameter name="name" class="java.lang.String" />
<parameter name="surname" class="java.lang.String" />
<background>
..
If name and surname shall not be filled by parameters but by your datasource you'd need to register them as fields and refer to them as such.
..
<property name="ireport.y" value="24"/>
<field name="name" class="java.lang.String" />
<field name="surname" class="java.lang.String" />
<background>
..
in the text fields you refer to them as $F{name}, $F{surname}

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..
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.
In the subreport variable, you have calculation=System. This should be used when you are calculating the variable yourself using a scriptlet. You do not have an appropriate scriptlet attached to the report, so the variable is never being calculated.
You probably want to change the variable definition so that it is calculated at the start of the subreport, something like this:
<variable name="A" class="java.lang.Integer" resetType="Report" calculation="Nothing">
<variableExpression><![CDATA[$F{sum}]]></variableExpression>
</variable>
resetType="Report" will never reset the variable while the report is running.
calculation="Nothing" will evaluate the variableExpression for every row in the dataset.
These attributes could be omitted though, as they are the default values used. You should then find that the variable is initialised correctly.
From the JasperReports Ultimate Guide:
The value coming from the subreport is available only when the whole band containing the subreport is printed. If you need to print this value using a textfield placed in the same band as your subreport, set the evaluation time of the textfield to Band
i am agree with #bubba_hego99, the calculation type as below:
system no caculation, just set as system, or valued as other operator.
nothing no calulation, just set the variable.
sum execute sum($Feild("A")).
....
The detail you want to know other caculation, you should be to see the ireport document.
Note, in earlier versions of JasperReports:
When using a subreport returnValue, the variable returned must not be in the same band as the subreport itself.
The affectation is done I think at the end of the rendering of the band.
So add another band and use your variable inside.