I am using iReport 4.0.2 and I want to show a result in my main report. For example, in my main report we have two columns and I want to get the sum of that two columns, just like this format:
A B sum
10 5 15
Where A is one field in the main report and B is a return value of my subreport.
This works well. But, the key point is that sometimes subreport will not return any value, which is the problem. In this case, the result of sum is like this:
A B sum
10 NULL
As we see here, B is the subreport return value but it's value neither NULL nor 0 . That's why we have that problem.
I try to find how can I get the return value from subreport when the SQL returns no results. I know iReport has a property named 'When No Data', but it doesn't help.
So I want to know, whether we have another way to solve the problem in iReport or using some SQL skills.
I think that in your second example B is actually null but perhaps your text field has the 'Blank When Null' property set. I was able to reproduce your results except for the blank for B.
The real issue is that you can't add or subtract with a null value. JasperReports will just print null. You have to add some logic to provide a default value if one of the variables in the expression is null. For example
$F{MAIN_REPORT_FIELD} - ($V{SUB_RESULT} == null ? 0 : $V{SUB_RESULT})
Unfortunately, this is broken for reports that use Groovy for expressions in JasperReports 4.0.2 (Case 0005138) and will always return null.
You have a few options:
You could upgrade to JasperReports/iReport 4.1.1 which resolves this issue.
You could switch to using Java for expressions.
Depending on your report this will either be painless or it will be a chore. Any fields that don't have the correct Expression Class will have to be fixed up and you'll probably have to be more explicit in any expression that performs operations on non primitive/wrapper types.
if you are doing sum of main field and sub report field in main report then in the sum expression (main report field + (subreportfield==null ? 0 : subreportfield) .
return subreport values to main report variable.
Then do the sum of the main report field and bareport return variable.
Do the same condition as given above.
Related
I am trying to do a simple expression in iReport, but whatever I try seems hasn't given me which I expected. It seems like such a simple thing to do, but I couldn't understand what's happening with my code.
All I'm trying to do is show a subreport when the value of an string field is a simple word like "VALUE". I am using the Print When Expression for the subreport control, however I'm not able to the see or reproduce this text with this expression. For me seems that I'm not passing well the parameters I wan't to use to do the print when expression in the subreport from the master report.
Is a disclaimer, so in master report I have a simple subreport, with the same conection from master and one parameter called Entity = "_id.Entity" (we use mongodb to get the fields for the report with a $group) and in the subreport part I have 2 text fields, the first one have the print when expression when P{Entity}.equals("VALUE"), the second one don't have any condition so this works perfectly, but the textfield with the condition doesn't work.
Any help would be appreciated. Thanks.
Is there a way to show one subreport if the other subreport doesn't have any rows (doesn't return any rows).
What have I tried:
In my first subreport I returned the number of processed rows "REPORT_COUNT" to master report variable "SUBREPORT_VAR". Variable "SUBREPORT_VAR" works as it should. I added additional text field to master report and output variable to it. I set Evaluation time of text field to Band, as without this it doesn't work correctly.
Further I set option "Print When Expression" of the second subreport to:
$V{SUBBREPORT_VAR} < 1
I use jasper studio 5.5.0 final.
If there is no row count wouldn't the result be null.
If so you you could use this in the print when expression in your second subreport:
$V{SUBBREPORT_VAR}==null
another option is to run the query in your main report and use this as the subreport expression:
IF($F{SOME_VAR_FROM_YOUR_QUERY}==null,subreport2.jasper,subreport1.jasper
This prints subreport2 when your SOME_VAR_FROM_YOUR_QUERY returns null if not it prints subreport1.
I've made a crystal report which has a section with name "detail d", and would like to suppress this section if the field "stock_gift" is empty (i.e. "")
I've set the condition in the section expert using below condition:
(trim({gift.stock_gift}) = "" or isnull({gift.stock_gift}))
But the section isn't suppressed, I checked the database whether the field is an empty string and it shows it's an empty string:
Is there anything wrong with the formula that caused the section unable to suppress?
Are you sure that {gift.stock_gift} contains the empty string and NOT a null value? If it's a null then your suppression formula won't work correctly as it is; you need to either swap the order of your boolean statements like this:
isnull({gift.stock_gift}) or trim({gift.stock_gift}) = ""
Or get rid of everything except for the isnull() check. The reason for this is that if CR encounters a null value in a formula and it's not handled via isnull() as the very first thing in the formula, then that formula will produce an error exception and will not continue to be evaluated.
I am using Jaspersoft Studio for building reports.
I have a variable which actually checks a column TODAY has null values of not. Expression of that value is
$F{TODAY} == null ? new BigDecimal(0.00) : $F{TODAY}
TODAY column has null values in my case for now.
The problem is first the expression above returns null for the first row and zero for the other rows. It should return 0 for every row since TODAY column is null
What can be the reason for that?
The problem is leaving "initial value expression" part empty.
I added the same expression also to that field and it works fine now.
Same problem here. The older fix of use same expression for Initial Value does not work for me, because the first two rows now have same value.
I end up fix it by on iReport, changing the Variable:Property:Reset Type: to None.
i had a similar problem and i fixed it by setting 'evaluation time' of the 'Text Field' showing the variable to 'Auto' instead of 'Now' in jasperSoft Studio
I'm having problem with JasperReports report by getting sum of two fields.
I have created so far variables that holds fields "TareWeight" variable that is casting values from double to float
new Float($F{EquipmentTareWeightKg})
"CargoWt" variable that is casting from string to float, etc...
Float.parseFloat($F{UfvFlexString03})+Float.parseFloat($F{UfvFlexString04})
So now I have two variables which I can manipulate with.
The problem starts when I wish to do a "recap" using crosstab. I have setup two groups that is done with wizard, and added another in crosstab. (please refer to the printscreen below)
And now, I have created under measures SumOf Tare that is using variable TareWt, and SumOfCargoWt that is using variable CargoWt.
I have tried to create another Measure SumOfGrossWt that would do a sum of variables TareWt + CargoWt but I have got some nonsense values.
I have tried to do another approach by creating variable GrossWt that would basically do exact the same thing as previous attempt
$V{Tare}+$V{CargoWt}
or use an variable: GrossWt
$V{Tare}+$V{CargoWt} or new Float($F{EquipmentTareWeightKg})+(Float.parseFloat($F{UfvFlexString03})+Float.parseFloat($F{UfvFlexString04}))
None of these actually worked: (Sum should be done by "Sum Of Tare"+ "Sum of CargoWt" = "Sum of GrossWt") plese refer to the second picture.
Here is a complete jrxml code/file:
jrxml file
save it as *.jrxml
Can please someone help me find a solution? how can I sum those two measure fields and get correct values?
Since CargoWt field had some null values in database, addition float value with null is equal to null, so the only value which was displayed on report are the one that had values for CargoWt, all others with null value were "ignored".