Search code examples
jasper-reports

iReport parameter not being informed by field


I am using iReport - my first time using it - and I am trying to make a report that will involve passing a parameter to a subreport. I am not able to get that to work, so I did the following test:

  1. I have a large query that grabs various Fields from my DB - when I preview the report these fields are being pulled and display (over and over, etc., a lot of stuff is being pulled).
  2. I created a new parameter on the main report called "myparam". I made it's default value one of the fields that is being successfully pulled from my DB.
  3. I created a textfield to display this param - the intent being it will echo the field that displays already in my report. If I can get it to do that, I figure I can get it to correctly "plug in" to my subreport.

The issue is - when I preview, I am asked to give the parameter a value - I choose default, which defaults it to the Field I set, which means it should now echo that field (aka I WANT it to echo that field). But it does not echo that field. Instead it just shows null over and over again. Here is a screenshot of what I am talking about

Here is the setup for my report:

The setup for my report

Here is proof I've set the default value in my xml:

    <parameter name="myparam" class="java.lang.String">
    <defaultValueExpression><![CDATA[$F{TCORDERID}]]>  </defaultValueExpression>
</parameter>

Here is a snippet of the report which is being generated incorrectly (aka myparam is not getting populated right).

The resulting report which is not correct

THEY ARE BOTH STRINGS


Solution

  • From my understanding of parameters, they are passed to the report/subreport when it gets created. You have set the default value of the parameter to one of your fields, which has not been populated yet when the report gets generated.

    I would suggest adding a simple subreport (which is the reason you want to use parameters anyway), remove the parameter on the main report, and add it to the subreport (with a text field to display it).

    Now, add the Parameter to the subreport object in your main report. (This is under the Subreport properties section in iReport.

    Name: The name of the Parameter in the subreport.

    Expression: The field from your main report that will be passed to the subreport.

    So your main report will have the following under the subreport detail:

    <subreportParameter name="myparam">
      <subreportParameterExpression><![CDATA[$F{TCORDERID}]]></subreportParameterExpression>
    </subreportParameter>
    

    And then the Parameter in the subreport:

    <parameter name="myparam" class="java.lang.String">
        <defaultValueExpression><![CDATA["No result"]]></defaultValueExpression>
    </parameter>