How To Handle Divide By Zero Error In Asp.net
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Divide by zero error, how do I fix this? up vote 7 down vote favorite 1 C# novice here, when the int 'max' below is 0 I get a divide by zero error, I can see why this happens but how should I handle this when max is 0? position is also an int. private void SetProgressBar(string text, int position, int max) { try { int percent = (100 * position) / max; //when max is 0 bug hits string txt = text + String.Format(". {0}%", percent); SetStatus(txt); } catch { } } c# divide-by-zero share|improve this question edited Oct 1 '12 at 9:54 Aziz Shaikh 11.6k73954 asked Oct 1 '08 at 22:55 Scott You may use a safe division method like the one available at stackoverflow.com/a/13260217/378115 –Julio Nobre Nov 6 '12 at 22:32 add a comment| 10 Answers 10 active oldest votes up vote 15 down vote int percent = 0 if (max != 0) percent = (100*position) / max share|improve this answer edited Oct 1 '08 at 23:01 rjzii 10.1k75996 answered Oct 1 '08 at 22:57 Simon 22.6k2274110 add a comment| up vote 8 down vote Well, that entirely depends on the behaviour you want. If the maximum value of your program bar is zero, is it full? Is it empty? This is a design choice, and when you've chosen, just test for max == 0 and deploy your answer. share|improve this answer answered Oct 1 '08 at 22:57 Adam Wright 39.9k8102137 add a comment| up vote 6 down vote You can throw an exception. You can do int percent = ( max > 0 ) ? (100 * position) / max : 0; You can choose to do nothing instead of assigning a value to percent. many, many other things... Depends on what you want. share|improve this answer answered Oct 1 '08 at 22:57 Swati 13.8k42551 add a comment| up vote 3 down vote Check for zero. if ( max == 0 ) { txt = "0%"; } else { // Do the other stuff.... share|improve this answer answered Oct 1 '08 at 22:57 palehorse 13.6k23144 add a comment| up vote 2 down vote This is not a C# problem, it's a math problem. Division by zero is undefined. Have an if statement that checks whether m
& Samples Assembly & Controls Community Developer Sites Downloads Hosting Services Introduction Knowledge Base Sample Chapters WebCasts ASP.NET Applications http://www.devasp.net/net/articles/display/929.html Articles & Samples ASP.Net Sites Assembly & Controls Downloads Errors & Bugs Introduction Knowledge Base Sample Chapters WebCasts VB.Net Articles & Samples Downloads Errors, Bugs & Fixes Introduction http://forums.asp.net/t/1769101.aspx?How+to+avoid+divide+by+zero+error+in+calculating+totals+on+the+fly Knowledge Base Sample Chapters WebCasts C-Sharp Applications Articles & Samples C-Sharp Sites Errors, Bugs & Fixes Introduction Knowledge Base Sample Chapters WebCasts SQL Server Applications Articles & Samples how to SQL Sites Downloads Errors, Bugs & Fixes Introduction Knowledge Base Sample Chapters WebCasts How to handle divide by zero exception using “DivideByZeroException” class in VB and C# (.net).Author: Shehzad HemaniDownload Source Code : 929_code.zipIn this simple article you will learn how we can handle divide by zero exception using “DivideByZeroException” class in VB and how to handle C# (.net). DivideByZeroException: This class is used to handle the exception which occurs when a number is divided by a zero. To handle this exception .net provides us a class named DivideByZeroException. DivideByZeroException Handling: We can handle divide by zero exception by writing try catch statement and passing DivideByZeroException object. To demonstrate make a window application. Drag one button and two text boxes on form. Enter values in text boxes and press button. Now write the following code on Button click event: C# private void btn_divide_Click(object sender, EventArgs e) { int a =Convert.ToInt16(textBox1.Text); int b = Convert.ToInt16(textBox2.Text); try { int sum=a/b; MessageBox.Show(sum.ToString()); } catch (DivideByZeroException ex) { MessageBox.Show("Exception Occured Do Not Divide By Zero"); } } VB Private Sub btn_divide_Click(ByVal sender As Object, ByVal e As EventArgs) Dim a As Integer = Convert.ToInt16(textBox1.Text) Dim b As Integer = Convert.ToInt16(textBox2.Text) Try Dim sum As Integer = a / b
ASP.NET Community Standup Forums Help Home/ASP.NET Forums/Data Access/SQL Server, SQL Server Express, and SQL Compact Edition/How to avoid divide by zero error in calculating totals on the fly How to avoid divide by zero error in calculating totals on the fly [Answered]RSS 3 replies Last post Feb 14, 2012 01:35 AM by jeeveshfuloria08 ‹ Previous Thread|Next Thread › Print Share Twitter Facebook Email Shortcuts Active Threads Unanswered Threads Unresolved Threads Support Options Advanced Search Reply Chumley Walr... Member 20 Points 275 Posts How to avoid divide by zero error in calculating totals on the fly Feb 13, 2012 03:17 PM|Chumley Walrus|LINK I'm trying to display the winning PCT of each team in a league where as I calculate their wins (sum(SU)) and losses (sum(SULOSS) . I can't get around the "can't divide by zero" error if the win or loss total contains a zero still: select team,sum(SU) as W,sum(SULOSS) as L,sum(SU)/sum(SU) + sum(SULOSS) as PCT from ppigL where team in (select team from ppigteam where league = 'mtw') and gmdate < '10/1/2009' group by team ?? TIA chumley Reply rajsedhain Contributor 2601 Points 1018 Posts Re: How to avoid divide by zero error in calculating totals on the fly Feb 13, 2012 04:00 PM|rajsedhain|LINK you can do something like this: select case when sum(SU) = 0 then null else sum(SU)/sum(SU) end or you can use nullif Select team,sum(SU) as W,sum(SULOSS) as L,sum(SU)/nullif(sum(SU), 0) + sum(SULOSS) as PCT from ppigL where team in (select team from ppigteam where league = 'mtw') and gmdate < '10/1/2009' group by team Thanks, Raj Sedhain Reply sandeepmitta... Contributor 5754 Points 1163 Posts Re: How to avoid divide by zero error in calculating totals on the fly Feb 13, 2012 09:46 PM|sandeepmittal11|LINK SELECT team, sum(SU) as W,sum(SULOSS) as L ,CASE WHEN ISNULL(sum(SU),0) = 0 THEN 0 ELSE ISsum(SU)/sum(SU) END + sum(SULOSS) as PCT FROM ppigL where team in (select team from ppigteam where league = 'mtw') and gmdate < '10/1/2009' group by team Sandeep Mittal | Tech Blog : IT Developer Zone | Twitter : @itdeveloperzone | Facebook : @itdeveloperzone Reply jeeveshfulor..