GetScalar() - Runtime Error

Posts   
 
    
saravana
User
Posts: 63
Joined: 11-Nov-2010
# Posted on: 19-Dec-2011 09:20:07   

Hi,

When I tried to get the max value of a field using the GetScalar() method in a empty table throws runtime error. I want to return zero if there is no data in the table. The current code(Adapter) is as follows:

var maxLId = localAdapter.GetScalar(LanguageFields.Lid, AggregateFunction.Max);
maxLocalLanguageId = Convert.ToInt16(maxLId);

Is any other way to return zero if no rows are present in the table?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 19-Dec-2011 09:43:09   

I guess that's a null exception. No records would return a null, so you have to check for that.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39908
Joined: 17-Aug-2003
# Posted on: 19-Dec-2011 09:58:11   

var maxLId = localAdapter.GetScalar(LanguageFields.Lid, AggregateFunction.Max) ?? 0;

Frans Bouma | Lead developer LLBLGen Pro