Solved

Overflow Division by Zero

Posted on 2008-10-30
3
622 Views
Last Modified: 2011-09-20
I am trying to figure out why is that my code gets an overflow, I know somehow is dividing by a null or zero value , but I want it to actually get the value from a variable.

I think the error is whitin line 163 , so I commented out
set rs = Server.CreateObject("ADODB.Recordset")
sql = "SELECT * from Scores WHERE SSN = '" & session("username") & "'"
rs.Open sql,conSQL,3,3
if rs.recordcount = 0 then trigger = 2
					'---------------------------------------
if trigger <> 2 then
		dim eagleCount, birdieCount, parCount, bogeyCount, doubleCount, otherCount, totalCount
		'right here...............dim avg18, low18, high18
		eagleCount = 0
		birdieCount = 0
		parCount = 0
		bogeyCount = 0
		doubleCount = 0
		otherCount = 0
		totalCount = 0
		
		rs.movefirst
		while not rs.eof
			call breakdown
			rs.movenext
		wend
	
	
		dim numRecords18
		dim numRecords9
		dim average18
		dim average9
		dim lowRound18
		dim lowRound9
		dim highRound18
		dim highRound9
		
	call findCount("Full", "Full", numRecords18)
	call findAverage("Full", "Full", numRecords18, average18)
	call findLowest("Full", "Full", lowRound18)
	call findHighest("Full", "Full", highRound18)
	call findCount("Front", "Back", numRecords9)
	call findAverage("Front", "Back", numRecords9, average9)
	call findLowest("Front", "Back", lowRound9)
	call findHighest("Front", "Back", highRound9)
	
		dim average3
		dim average4
		dim average5
		
		call findAllTHIS(3, finalAverage)
		average3 = finalAverage
		call findAllTHIS(4, finalAverage)
		average4 = finalAverage
		call findAllTHIS(5, finalAverage)
		average5 = finalAverage
end if				'---------------------------------------
	
sub breakdown()
	dim startHere
	dim endHere
	dim difference
	dim hole
	
		if rs("Round") = "Full" then 
			startHere = 1
			endHere = 18
		elseif rs("Round") = "Front" then
			startHere = 1
			endHERE = 9
		else
			startHERE = 10
			endHERE = 18
		end if
		
		set rs2 = Server.CreateObject("ADODB.Recordset")
		sql2 = "SELECT * from CourseHoles WHERE CourseID =" & rs("CourseID") & " AND Difficulty = 'Par'"
		rs2.Open sql2, conSQL
		
		for hole = startHere to endHere
			difference = cint(rs("H" & hole)) - cint(rs2("h" & hole))
			select case difference
				case -2
					eagleCount = eagleCount + 1
				case -1
					birdieCount = birdieCount + 1
				case 0
					parCount = parCount + 1
				case 1
					bogeyCount = bogeyCount + 1
				case 2
					doubleCount = doubleCount + 1
				case else
					otherCount = otherCount + 1
			end select
		next
		totalCount = eagleCount + birdieCount + parCount + bogeyCount + doubleCount + otherCount
		rs2.close
		set rs2 = nothing
end sub	
	
sub findCount(theCase1, theCase2, byref numRecords)
	numRecords = 0
	rs.movefirst
	while not rs.eof
		if rs("Round") = theCase1 or rs("Round") = theCase2 then numRecords = numRecords + 1
		rs.movenext
	wend
end sub
 
 
sub findAverage(theCase1, theCase2, theCount, byref theAverage)
	dim total
	total = 0
	rs.movefirst
	while not rs.EOF
		if rs("Round") = theCase1 or rs("Round") = theCase2 then total = total + cint(rs("Total"))
		rs.movenext
	wend
	if thecount = 0 then theAverage = 0 else theAverage = total/theCount
end sub
 
sub findLowest(theCase1, theCase2, byref theLowest)
	dim lowest
	rs.movefirst
	lowest = 1000
	while not rs.eof
		if rs("Total") < lowest and (rs("Round") = theCase1 or rs("Round") = theCase2) then lowest = rs("Total")
		rs.movenext
	wend
	if lowest = 1000 then theLowest = "-" else theLowest = lowest
end sub
 
sub findHighest(theCase1, theCase2, byref theHighest)
	dim highest
	rs.movefirst
	highest = -1000
	while not rs.eof
		if rs("Total") > highest and (rs("Round") = theCase1 or rs("Round") = theCase2) then highest = rs("Total")
		rs.movenext
	wend
	if highest = "-1000" then theHighest = "-" else theHighest = highest
end sub
 
sub findAllTHIS(findTHIS, finalAverage)
		dim grandTotal
		dim totalCountTHIS
		grandTotal = 0
		totalCountTHIS = 0
		rs.movefirst
		while not rs.eof
		recordTotal = 0
		countTHIS = 0
			call findTHISaverage(findTHIS, recordTotal, countTHIS)
			grandTotal = grandTotal + recordTotal
			totalCountTHIS = totalCountTHIS + countTHIS
			rs.movenext
		wend
	'I have to look at here to see why in dividing by a zero value
	
		finalAverage = grandTotal/totalCountTHIS
end sub
 
sub findTHISaverage(findTHIS, recordTotal, countTHIS)
	dim startHere
	dim endHere
	
 
		if rs("Round") = "Full" then 
			startHere = 1
			endHere = 18
		elseif rs("Round") = "Front" then
			startHere = 1
			endHERE = 9
		else
			startHERE = 10
			endHERE = 18
		end if
		
		set rs2 = Server.CreateObject("ADODB.Recordset")
		sql2 = "SELECT * from CourseHoles WHERE CourseID =" & rs("CourseID") & " AND Difficulty = 'Par'"
		rs2.Open sql2, conSQL
		
		for hole = startHere to endHere
			if rs2("h" & hole) = findTHIS then 
				recordTotal = recordTotal + rs("H" & hole)
				countTHIS = countTHIS + 1
			end if
		next
		'response.write(recordTotal & "." & count3 & "..." )
		
		rs2.close
		set rs2 = nothing
end sub
 
		rs.close
		set rs = nothing
 
%>

Open in new window

0
Comment
Question by:RickyGtz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 2

Expert Comment

by:BobTheViolent
ID: 22843426
I highly recommend using some Try Catch blocks in your code, especially when there is the possibility of something so simple as a div by zero error.

Try something like this code:


Try
 
  finalAverage = grandTotal/totalCountTHIS
 
Catch ex As System.DivideByZeroException
 
  finalAverage = 0 'Set it to 0 or you could do something else here...
 
End Try

Open in new window

0
 

Author Comment

by:RickyGtz
ID: 22843853
I dont think vbasic has try catch statments. I am using classic asp and visual basic
0
 
LVL 2

Accepted Solution

by:
BobTheViolent earned 333 total points
ID: 22844180
Sorry didn't know you were using that.  Classic ASP / VB I believe uses the following method of catching errors.

Note you can also use these two:  
On Error Resume Next ' Ignores errors and moves on to the next line of code
On Error Goto 0 'Goes back to the default "Blow up and crash mode"

But I recommend this:

Sub
On Error Goto errorcatch 'This starts error handling and says go to this tag name.
 
 finalAverage = grandTotal/totalCountTHIS
 
 
exit sub 'This makes sure to end your subroutine if there are no errors or it will keep executing lines happily down into the error checking code.  You only want to run this if there is an error.
errorcatch:
 
 finalAverage = 0 'Or something else here
 On Error Goto 0 ' Reset the error handler
End Sub

Open in new window

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question