question

Vamshi-5948 avatar image
0 Votes"
Vamshi-5948 asked Viorel-1 answered

Handle NULL scenario.

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.

dotnet-csharpdotnet-aspnet-core-mvcdotnet-aspnet-core-webapi
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @Vamshi-5948,

Could you please tell me how you use appCountQuery to query the database? Could you please share related codes? According to your description, it seems the query's result will return null instead of the int.

1 Vote 1 ·

1 Answer

Viorel-1 avatar image
1 Vote"
Viorel-1 answered

Did you try this class?

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

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for the inputs.

0 Votes 0 ·