Error on site with mathematical operation on ASP .NET core

Viewed 37

I have count and percentage of some things from my databases and show them on site.( with mathematical operation) But If database be empty , I see this error:

adminContentCount.UserPercentage = (currentMonthUser * 100) / userCount; this is my service:

 CountViewModel adminContetnCount = new CountViewModel()
        {
            
           UserCount = await _userRepository.UserCount(),
       
            CurrentMonthUser = await _userRepository.CurrentMonthUser(),

      
        };

          

and this is my controller:

public async Task<IActionResult> Index()
    {
        var adminContentCount = await _siteService.AdminContentCount();
    
        var userCount = adminContentCount.UserCount;
        var currentMonthUser = adminContentCount.CurrentMonthUser;

        return View(adminContentCount);
    }

I use it on service and on controller and then will be show On view

How can I check this on my service. I get count of them and percentage from database.

1 Answers

I don't see your error posted here, but I'm assuming it's a divide by 0. Therefore

adminContentCount.UserPercentage = userCount == 0 ? 0 : (currentMonthUser * 100) / userCount;
Related