hasti.au
hasti.au

Reputation: 91

Error on site with mathematical operation on ASP .NET core

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.

Upvotes: 1

Views: 67

Answers (1)

Nikki9696
Nikki9696

Reputation: 6348

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;

Upvotes: 2

Related Questions