GetScalar returns null

Posts   
 
    
JayBee
User
Posts: 277
Joined: 28-Dec-2006
# Posted on: 28-May-2024 22:56:10   

I am using SelfServicing, develop for dotnet 4.8 and using LLBLGenP Pro v5.8

Hi,

I have a parent table and a child table and each entry in that table can have 0:n children. Each child has a Number. For each parent I have to present the sum of the Numbers of the children. I am using the following code:

(int) childCollection.GetScalar(ChildFieldIndex.Number, null, AggregateFunction.Sum, filter);

The result returns a System.InvalidCastException in the situation that there are no Child records. I was expecting to get 0 in that situation. What is the proper way to achieve this?

Kind regards,

Jan

Walaa avatar
Walaa
Support Team
Posts: 14975
Joined: 21-Aug-2005
# Posted on: 29-May-2024 05:02:09   

Remove the (int) casting and check the output in that case, if it's null, then you need to check on null before casting to int, or cast to int?

JayBee
User
Posts: 277
Joined: 28-Dec-2006
# Posted on: 29-May-2024 08:24:34   

I tried casting it to int? but get the same error. The response of GetScalar isn't null, it is System.DBNull. I'll check for that first and then do the casting.