Handle NULL scenario.

Vamshi 151 Reputation points
2022-07-01T04:29:59.533+00:00

Hi,

currently var appCountResult throws exception when the result is NULL because it accepts INTEGER.

Need inputs to change the var appCountResult condition so that it can accept to return NULL (or) INTEGER value based on the var appCountQuery .

AppResponse.cs file:

public class AppCount
{
public int Count { get; set; }
}

Helper.cs file:

var appCountQuery = $"(SELECT MAX(COUNT) AS COUNT FROM (SELECT COUNT(DISTINCT WN.APPKEY) AS COUNT FROM DEV.DBO.VW_NUMBER WN
WHERE WN.ANUMBER IN ('MLSR12677','S448904')" +
$" AND WN.UNITKEY IN (SELECT IC.UNITKEY FROM DEV.DBO.VW_DATA IC WHERE IC.UNITNAME = 'MAS') " +
$" GROUP BY WN.DEPTKEY, WN.ANUMBER HAVING COUNT(DISTINCT WN.APPKEY) = 1) X )";

var appCountResult = (await _cache.GetAsync(agents_CacheKeyPrefix,
TimeSpan.FromMinutes(CACHE_TIME),
() => _Client.QueryAsync<AppCount>(appCountQuery, ct: _cancellationToken).ConfigureAwait(false).GetAwaiter().GetResult()
).ConfigureAwait(false)).FirstOrDefault();

     if ( appCountResult == null )  
            {  
                return new AppResponse()  
                {                          
                    Title = "No app count."  
                };  
            }  
            else if (agentCountResult.Count > 1)  
           {  
               return new AppResponse()  
                {                          
                    Title = "Unable to retrieve app data due to conflict."  
                };  
           }  
           else  
           {  
                return new AppResponse()  
                {                          
                    Title = "No app data."  
                };  
           }  
            

Thank you.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,159 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,238 questions
{count} votes

Accepted answer
  1. Viorel 112.1K Reputation points
    2022-07-01T13:57:18.787+00:00

    Did you try this class?

    public class AppCount  
    {  
       public int? Count { get; set; }  
    }  
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful