new to report builder, I'm trying to solve an issue where the grand total in the report is showing #error instead of 0. Error in Grand Total
Here's the expression for the field:
=Code.Divide(Microsoft.VisualBasic.Strings.FormatNumber(Sum(Fields!InExtCost.Value, "ClassDesc"), 5), Microsoft.VisualBasic.Strings.FormatNumber(Sum(Fields!InTranQty.Value, "ClassDesc"), First(Fields!DecimalsQuantity.Value, "RptParameter")))
Divide is the custom code written:
Public Function Divide(ByVal dividend As Double, ByVal divisor As Double) As Double
If IsNothing(divisor) Or divisor = 0 Or IsNothing(dividend) Or dividend = 0 Then
Return 0
Else
Return dividend / divisor
End If
End Function
I've tried using the code to run 0 divide by 1 and 1 divide 0 and both of them shows up 0 in the report preview, I'm expecting it to show up 0 if either dividend or divisor is 0.
Maybe there are NULL values causing the error.
In any case i created a sample dataset with a mix of values
SELECT 50.123456789 AS InExtCost, 10.123456789 As InTranQty UNION ALL
SELECT 50.123456789 AS InExtCost, 0 As InTranQty UNION ALL
SELECT 0 AS InExtCost, 10.123456789 As InTranQty UNION ALL
SELECT 0 AS InExtCost, 0 As InTranQty UNION ALL
SELECT NULL AS InExtCost, 0 As InTranQty UNION ALL
SELECT 0 AS InExtCost, NULL As InTranQty UNION ALL
SELECT 1 AS InExtCost, NULL As InTranQty UNION ALL
SELECT NULL AS InExtCost, 1 As InTranQty
In your expression I have added a conversion to double using Cdbl function
=Code.Divide( Microsoft.VisualBasic.Strings.FormatNumber(Sum(Cdbl(Fields!InExtCost.Value), "ClassDesc"), 5)
, Microsoft.VisualBasic.Strings.FormatNumber(Sum(Cdbl(Fields!InTranQty.Value), "ClassDesc"), First(Fields!DecimalsQuantity.Value, "RptParameter"))
)
Now that each parameter your are passing to the Divide function is double you can simplify it (or leave it as it is)
Public Function Divide(ByVal dividend As Double, ByVal divisor As Double) As Double
If divisor = 0 Then
Return 0
Else
Return dividend / divisor
End If
End Function