locked
System.DivideByZeroException: 'Attempted to divide by zero RRS feed

  • Question

  • User-164452226 posted

    Good day Tech Gurus,

    l am following a tutorial by some genius, a project called sportsStore that was done using asp mvc 5 in visual studio 2013. I am following it through using visual studio 2017. Now l am getting the error mentioned on the subject. Here is what l have done so far:

    //From SportsStore.Domain.Entities
    
    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SportsStore.Domain.Entities { public class Product { public int ProductID { get; set; } public string Name { get; set; } public string Description { get; set; } public decimal Price { get; set; } public string Category { get; set; } } } //From SportsStore.WebUI Controller using System; using System.Collections.Generic; using System.Linq;
    using System.Web; using System.Web.Mvc; using Moq; using SportsStore.Domain; using SportsStore.Domain.Abstract; using SportsStore.Domain.Concrete; using SportsStore.Domain.Entities; using SportsStore.WebUI.Models; using SportsStore.UnitTests; namespace SportsStore.WebUI.Controllers { public class ProductController : Controller { // GET: Product // private EFProductRepository repository; public int PageSise = 4; public int PageSize { get; set; }//For unit tests public ViewResult List(int page = 1){ EFProductRepository repository = new EFProductRepository(); ProductsListViewModel model = new ProductsListViewModel() { Products = repository.Products .OrderBy(p => p.ProductID) .Skip((page - 1) * PageSize) .Take(PageSize), PagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = repository.Products.Count() } }; return View(model); } } } //From SportsStore.WebUI View @using SportsStore.Domain.Entities @using SportsStore.WebUI.HtmlHelpers @using SportsStore.WebUI.Models @model SportsStore.WebUI.Models.ProductsListViewModel @{ ViewBag.Title = "Products"; } @foreach (var p in Model.Products) { <div class="well"> <h3><strong>@p.Name</strong><span class="pull-right label label-primary">@p.Price.ToString("c")</span></h3> <span class="lead"> @p.Description</span> </div> } <div class="btn-group pull-right"> @Html.PageLinks(Model.PagingInfo, x => Url.Action("List", new { page = x })) </div> //From SportsStore.WebUI Models/PageINfo using System; using System.Collections.Generic; using System.Linq;
    using System.Web; namespace SportsStore.WebUI.Models { public class PagingInfo { public int TotalItems { get; set; } public int ItemsPerPage { get; set; } public int CurrentPage { get; set; } public int TotalPages { get { return (int)Math.Ceiling((decimal)(TotalItems) / ItemsPerPage); }//THIS IS WHERE THE ERROR IS COMING FROM } } } //From SportsStore.WebUI ProductsListViewModel using System;
    using System.Collections.Generic; using System.Linq; using System.Web; using SportsStore.Domain.Entities; using SportsStore.UnitTests; namespace SportsStore.WebUI.Models { public class ProductsListViewModel { public IEnumerable Products { get; set; } public PagingInfo PagingInfo { get; set; } } }

    { get { return (int)Math.Ceiling((decimal)(TotalItems) / ItemsPerPage); }//THIS IS WHERE THE ERROR IS COMING FRO

    Please assist, l want to display products on pages but the ERROR is coming from this section: { get { return (int)Math.Ceiling((decimal)(TotalItems) / ItemsPerPage); }, which is a section in PagingInfo class.  Just edited the post to make the code more readable and for you guys to see where my problem is.

    Thank you in advance.

    Saturday, March 10, 2018 12:25 PM

Answers

  • User753101303 posted

    So #1 is ok and you run into issue #2. Try to add :

    tag.InnerHtml="Something";

    For now you create a <a Href="url"></a> rather than <a Href="url">Something</a> so links are created but they have no content to be shown.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 12, 2018 1:48 PM

All replies

  • User1120430333 posted

    Up on the toolbar icons, there is an icon that looks like what you see here {;}. The icon formats code correctly so when the post is submitted,  the source code kind of looks like what you seen in Visual Studio. You should use it in the future.

    In computer programming, you cannot divide a value by 0. It's an automatic exception error that is going to be thrown. There are many times that code you have downloaded from a tutorial is not up to snuff, and you have to debug it to make it work

    I suggest that you learn how to use the Visual Studio Debugger and find the line that is causing you program to blow-up and then find out what lead to the program blowing -up, which is called 'debugging'.

    https://msdn.microsoft.com/en-us/library/y740d9d3.aspx?f=255&MSPPError=-2147217396

    Saturday, March 10, 2018 3:45 PM
  • User409696431 posted

    You say the error is from :

    get { return (int)Math.Ceiling((decimal)(TotalItems) / ItemsPerPage); }

    If so, that means ItemsPerPage is zero, which it can't be for that equation to be valid.  You need to find out why it's zero, and if it really is possible for it to be zero,  modify the code to test for zero and handle the case properly.

    Sunday, March 11, 2018 3:22 AM
  • User-164452226 posted

    I have tried, l am failing to figure it out, that's why l had to post all the code. Earlier on l managed to display the products on one page, the challenge now is to have the products on different pages that can be navigated by clicking numbered links.

    Sunday, March 11, 2018 11:40 AM
  • User753101303 posted

    Hi,

    What if you try :

    // public int PageSise = 4;
    public int PageSize { get; set; } = 4;//For unit tests
    

    so that you have just a PageSize property whose value is 4. For now I suspect that PageSize in your code reads the property which is left to 0 rather than the field which is 4. Actually I never even thought to try to have both a field and a property with the same name.

    Sunday, March 11, 2018 12:19 PM
  • User-164452226 posted

    The error is gone but the problem now is l have to navigate by querying the browser.  http://localhost:50051/?page=1http://localhost:50051/?page=2  and so on. But a customer would not know that.

    Monday, March 12, 2018 6:57 AM
  • User-164452226 posted
    //What could be missing in this html helper code?? Its not displaying page links (eg.1 2 3), so l can click and navigate
    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.Web.Mvc; using SportsStore.WebUI.Models; namespace SportsStore.WebUI.HtmlHelpers { public static class PagingHelpers { public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pagingInfo, Func<int, string> pageUrl) { StringBuilder result = new StringBuilder(); for (int i = 1; i <= pagingInfo.TotalPages; i++) { TagBuilder tag = new TagBuilder("a"); tag.MergeAttribute("href", pageUrl(i)); if (i == pagingInfo.CurrentPage) { tag.AddCssClass("selected"); tag.AddCssClass("btn-primary"); } tag.AddCssClass("btn btn-default"); result.Append(tag.ToString()); } return MvcHtmlString.Create(result.ToString()); } } }

    Monday, March 12, 2018 7:29 AM
  • User753101303 posted

    Hi,

    Could it be that TotalPages is 0 ? When your code doesn't run as expected, use  https://docs.microsoft.com/en-gb/visualstudio/debugger/debugger-feature-tour to see what happens..

    Monday, March 12, 2018 8:05 AM
  • User-164452226 posted

    From all that l have posted, where can l put the break to start debugging, because it's showing no errors at all. It is displaying only the first page with the products but without page links.

    Monday, March 12, 2018 9:25 AM
  • User753101303 posted

    I would put a breakpoint at for (int i = 1; i <= pagingInfo.TotalPages; i++) to see if the lop is entered. If entered the other issue is that your "a" tags are empty (another quick thing to do for rendering issues is to use F12 or "view source" to check your HTML markup).

    The basic idea is that reading the code can show possible issues (but sometimes an error just can't be found by looking at the code). So it's likely  best to spend just few minutes in using those to understand what happens exactly

    Monday, March 12, 2018 10:36 AM
  • User-164452226 posted

    Step-into-results for code section:
    StringBuilder result = new StringBuilder();
    for (int i = 1; i <= pagingInfo.TotalPages; i++)
    {
    TagBuilder tag = new TagBuilder("a");
    tag.MergeAttribute("href", pageUrl(i));
    if (i == pagingInfo.CurrentPage)
    {
    tag.AddCssClass("selected");
    tag.AddCssClass("btn-primary");
    }
    tag.AddCssClass("btn btn-default");
    result.Append(tag.ToString());
    }
    return MvcHtmlString.Create(result.ToString());

    It’s getting into the loop but not to the return statement. I would have shown you all the screen shots l have compiled. Source code for the web page lacks the numbered links.

    <div class="btn-group pull-right">
    <a class="btn btn-default btn-primary selected" href="/Page1"></a><a class="btn btn-default" href="/Page2"></a><a class="btn btn-default" href="/Page3"></a>

    </div>

    Monday, March 12, 2018 12:38 PM
  • User753101303 posted

    So #1 is ok and you run into issue #2. Try to add :

    tag.InnerHtml="Something";

    For now you create a <a Href="url"></a> rather than <a Href="url">Something</a> so links are created but they have no content to be shown.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 12, 2018 1:48 PM
  • User-164452226 posted

    You are a star PatriceSc. Keep it up. This is what I did. 

    //Under PagingHelpers, l added 
                for (int i = 1; i <= pagingInfo.TotalPages; i++)
                {
                    TagBuilder tag = new TagBuilder("a");
                    tag.MergeAttribute("href", pageUrl(i));
                    if (i == pagingInfo.CurrentPage)
                    {
                        tag.AddCssClass("selected");
                        tag.AddCssClass("btn-primary");
                        tag.InnerHtml = (pageUrl(i));
                    }
                    tag.AddCssClass("btn btn-default");
                    result.Append(tag.ToString());
                }
                return MvcHtmlString.Create(result.ToString());
    
    //Then Under the view, List.cshtml
        <div class="btn-group pull-right">
            @Html.PageLinks(Model.PagingInfo, x => Url.Action("List", new { page = x+1 }))
        </div>
    
    //So it started showing links on the browser, /page2 and if click it navigates to page2 but showing link to page3 and so on.
    
    //I then decided to modify PagingHelpers, from:
    
     tag.InnerHtml = (pageUrl(i))
    
    //To
    
    tag.InnerHtml = ("Next");
    
    //Now the source code for that page link section is as follows
    
        <div class="btn-group pull-right">
            <a class="btn btn-default btn-primary selected" href="/Page2">Next</a><a class="btn btn-default" href="/Page3"></a><a class="btn btn-default" href="/Page4"></a>
           
        </div>
    //We thank God for this forum. May all contributors stay blessed.

    Tuesday, March 13, 2018 6:11 AM