easiest way to see if GetScalar returns a result

Posts   
 
    
yogiberr
User
Posts: 432
Joined: 29-Jun-2005
# Posted on: 18-Apr-2007 14:27:50   

version 1.0.2005.1 final (self-servicing) VS2005 asp.net 2.0


hiya,

I need to cast the result of a GetScalar to a decimal.Obviously, if no results are returned, then I'll get an error.

Below is my code, I have tried to check for null, but that doesn't seem to work.Maybe thre is something in-built that will let me do this?

Many thanks,

yogi


if  ((collOrderItems.GetScalar(CskStoreOrderItemFieldIndex.ProductId, CskStoreOrderItemFields.Quantity * CskStoreOrderItemFields.PricePaid, AggregateFunction.Sum, filtNonSpecialDelivery, relationsToUse, null))!= null) 
        {
        decimal NONspecialDeliveryTotal = (decimal)collOrderItems.GetScalar(CskStoreOrderItemFieldIndex.ProductId, CskStoreOrderItemFields.Quantity * CskStoreOrderItemFields.PricePaid, AggregateFunction.Sum, filtNonSpecialDelivery, relationsToUse, null);

    
        }

jmeckley
User
Posts: 403
Joined: 05-Jul-2006
# Posted on: 18-Apr-2007 14:52:13   

since it's an aggregation i think you will at least get zero (0) back.

decimal NONspecialDeliveryTotal = (decimal)collOrderItems.GetScalar(CskStoreOrderItemFieldIndex.ProductId, CskStoreOrderItemFields.Quantity * CskStoreOrderItemFields.PricePaid, AggregateFunction.Sum, filtNonSpecialDelivery, relationsToUse, null);

if not, this should work

object result = collOrderItems.GetScalar(CskStoreOrderItemFieldIndex.ProductId, CskStoreOrderItemFields.Quantity * CskStoreOrderItemFields.PricePaid, AggregateFunction.Sum, filtNonSpecialDelivery, relationsToUse, null);

decimal NONspecialDeliveryTotal = 0;
if(result == null)
{
     NONspecialDeliveryTotal = (decimal)result;
}
yogiberr
User
Posts: 432
Joined: 29-Jun-2005
# Posted on: 18-Apr-2007 17:30:31   

cheers Jason.

It doesn't seem to return a ZERO.

1 is there any way to make it return ZERO if no items are found? // PREFERRED OPTION

2 if not, then i suppose i'll have to try and use nulls, I'd really rather not have to do this.

I notice that the code that you supplied actually returns "system.DBNull"

object result = collOrderItems.GetScalar(CskStoreOrderItemFieldIndex.ProductId, CskStoreOrderItemFields.Quantity * CskStoreOrderItemFields.PricePaid, AggregateFunction.Sum, filtNonSpecialDelivery, relationsToUse, null);

I have no idea why.The problem is, if I replace "null" with "system.DBnull", i get an error:

'System.DBNull' is a 'type', which is not valid in the given context

I'm really struggling here.

Can anyone help?

many thanks,

yogi

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 18-Apr-2007 17:56:30   
I have no idea why.The problem is, if I replace "null" with "system.DBnull", i get an error

Please try System.DBNull.Value instead.

yogiberr
User
Posts: 432
Joined: 29-Jun-2005
# Posted on: 18-Apr-2007 18:14:27   

thanks Walaa,

that worked.I take it that the best I can hope for is to wrap the GetScalar method in a method that checks for "System.DBNull.Value"?

Is there no way that I can get the GetScalar method to return ZERO if no matches are found?

cheers,

yogi

jmeckley
User
Posts: 403
Joined: 05-Jul-2006
# Posted on: 18-Apr-2007 22:18:07   

getscalar returns a single value for any sql db type, not just numbers. zero would not be an appropiate default value if the scalar value is a string, date, or byte array.

therefore getscalar has to return an object. it is then up to the developer to determine how to handle the return value.

yogiberr
User
Posts: 432
Joined: 29-Jun-2005
# Posted on: 19-Apr-2007 10:54:22   

righto,

thanks for the confirmation.

yogi