Attempted to divide by zero error

Topics: Developer Forum
Sep 5, 2009 at 4:17 PM
Edited Sep 5, 2009 at 11:37 PM

I've downloaded the latest version of the source code. Version 58165.

 

When I do a compile, build, and publish from visual studio 2008 Team Developer to a local IIS website I've created I'm getting the following error when I try to browse to the poll page.

 

Server Error in '/' Application.
--------------------------------------------------------------------------------

Attempted to divide by zero.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.DivideByZeroException: Attempted to divide by zero.

Source Error:

Line 7:   <ul>
Line 8:   <% foreach(var option in ViewData.Model.PollOptions) {
Line 9:    var percentValue = Math.Round(((decimal)option.Votes / (decimal)total) * 100M); %>
Line 10:   <li id="option-<%= option.OptionID %>">
Line 11:    <h3><%= Html.Encode(option.OptionText) %></h3>
 

Source File: c:\Inetpub\TheBeerHouse\Views\Shared\Poll\PollResultItem.ascx    Line: 9

Stack Trace:


[DivideByZeroException: Attempted to divide by zero.]
   System.Decimal.FCallDivide(Decimal& result, Decimal d1, Decimal d2) +0
   System.Decimal.op_Division(Decimal d1, Decimal d2) +125
   ASP.views_shared_poll_pollresultitem_ascx.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in c:\Inetpub\TheBeerHouse\Views\Shared\Poll\PollResultItem.ascx:9
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +256
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
   System.Web.UI.Control.Render(HtmlTextWriter writer) +10
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
   System.Web.UI.Page.Render(HtmlTextWriter writer) +29
   System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +59
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1266

--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.3053; ASP.NET Version:2.0.50727.3053

 

If I try to browse to http://localhost/poll/index I'm getting the following error as well:

 

Server Error in '/' Application.
--------------------------------------------------------------------------------

The parameters dictionary contains a null entry for parameter 'page' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Index(System.Nullable`1[System.Boolean], Int32)' in 'TheBeerHouse.Controllers.PollController'. To make a parameter optional its type should be either a reference type or a Nullable type.
Parameter name: parameters 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentException: The parameters dictionary contains a null entry for parameter 'page' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Index(System.Nullable`1[System.Boolean], Int32)' in 'TheBeerHouse.Controllers.PollController'. To make a parameter optional its type should be either a reference type or a Nullable type.
Parameter name: parameters

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace: 


[ArgumentException: The parameters dictionary contains a null entry for parameter 'page' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Index(System.Nullable`1[System.Boolean], Int32)' in 'TheBeerHouse.Controllers.PollController'. To make a parameter optional its type should be either a reference type or a Nullable type.
Parameter name: parameters]
   System.Web.Mvc.ReflectedActionDescriptor.ExtractParameterFromDictionary(ParameterInfo parameterInfo, IDictionary`2 parameters, MethodInfo methodInfo) +158895
   System.Web.Mvc.<>c__DisplayClass1.<Execute>b__0(ParameterInfo parameterInfo) +18
   System.Linq.WhereSelectArrayIterator`2.MoveNext() +78
   System.Linq.Buffer`1..ctor(IEnumerable`1 source) +434
   System.Linq.Enumerable.ToArray(IEnumerable`1 source) +81
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +135
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +24
   System.Web.Mvc.<>c__DisplayClassa.<InvokeActionMethodWithFilters>b__7() +52
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +254
   System.Web.Mvc.<>c__DisplayClassc.<InvokeActionMethodWithFilters>b__9() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +254
   System.Web.Mvc.<>c__DisplayClassc.<InvokeActionMethodWithFilters>b__9() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +192
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +399
   System.Web.Mvc.Controller.ExecuteCore() +126
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +27
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7
   System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) +151
   System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext) +57
   System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext) +7
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75

 


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.3053; ASP.NET Version:2.0.50727.3053 

 

Any ideas?

Coordinator
Sep 6, 2009 at 5:55 PM

One of these values is 0

var percentValue = Math.Round(((decimal)option.Votes / (decimal)total) * 100M); %>

all you have to do is do a check to make sure there is values. Most likely it is the votes being 0 so... before you hit this line of code you could say if(option.Votes > 0) and that should resolve that issue. Hope this helps. This is also a known bug and will be resolved int he next release.

Sep 6, 2009 at 8:31 PM

Thanks for the prompt response.

As a temporary workaround I updated the SQL database PollOptions so that the default values would be atleast equal to 1.

UPDATE [C:\THEBEERHOUSE_MVC\THEBEERHOUSE\APP_DATA\THEBEERHOUSE.MDF].[TheBeerHouse].[PollOptions]
SET [Votes]='1'
WHERE [Votes]='0'
Go

This workaround let me load the poll screen and allows me to fully exercise the beer house app.

I'm using the beer house app as a demo application.

I'm building out a fully scripted WIX .MSI package which chains together the .MVC installer, creates the website, copies the files, and updates the IIS settings on both IIS6 and IIS7.

I'm hoping to contribute the WIX code back into the project here when I finish the work.