DIVIDE BY ZERO ERROR, WHEN RUN FROM ASP PAGE
am 12.04.2007 17:05:03 von joshdHere is the scenario, i have a stored procedure, it calculates
commissions and runs fine in SQL. However when the SP is run through
my asp page it will come back with a divide by zero error, but only
for one sales person... I have been through this code a hundred times
and can't find any errors. Any help would be much appreciated.
SP:
ALTER PROCEDURE [dbo].[SP_COMMISSIONS]
@SALESMAN VARCHAR(50),
@ACTION VARCHAR(50)
AS
-- SUM PAID
IF @ACTION='SUMPAID'
SELECT COALESCE(SUM(TOTALPAID),0) AS SUMPAID
FROM COMMLOG
WHERE PERSON=@SALESMAN
-- PAID REPORT
IF @ACTION='PAIDREPORT'
SELECT PERSON, NUMBER, DESCRIPTIO, MONTH(DATECOMPL) AS PDMONTH,
YEAR(DATECOMPL) AS PDYEAR, TOTAL_PROJ,
MARGIN, RATE, BASEPAID, MARGINPAID, TOTALPAID
FROM COMMLOG
WHERE PERSON=@SALESMAN AND
TOTALPAID!=0
ORDER BY YEAR(DATECOMPL) DESC, MONTH(DATECOMPL) DESC
-- NOT PAID REPORT PROJECTS
IF @ACTION='NOTPAIDREPORT'OR @ACTION='POSTCOMMISSIONS' OR
@ACTION='SUMNOTPAID'
SELECT A.PERSON, A.NUMBER, A.DESCRIPTIO, MONTH(A.DATECOMPL) AS
PDMONTH, YEAR(A.DATECOMPL) AS PDYEAR, A.TOTAL_PROJ,
ROUND(((A.TOTAL_PROJ-A.TOTALCOST)/A.TOTAL_PROJ*100),0) AS MARGIN,
COALESCE(B.RATE, 0) AS RATE,
(A.TOTAL_PROJ*.035) AS BASEPAID,
(COALESCE(B.RATE, 0)*A.TOTAL_PROJ/100) AS MARGINPAID,
((COALESCE(B.RATE, 0)*A.TOTAL_PROJ/100)+(A.TOTAL_PROJ*.035)) AS
TOTALPAID
INTO #TEMPCOMM
FROM
(SELECT PERSON, NUMBER, DESCRIPTIO, DATECOMPL, (TOTAL_PART
+TOTAL_LBR) AS TOTAL_PROJ, TOTALCOST
FROM PROJMASTSQL
WHERE NUMBER NOT IN (SELECT NUMBER FROM COMMLOG)
AND STATUS='D'
AND RIGHT(NUMBER,LEN(NUMBER)-6)NOT LIKE 'SC%'
AND TOTAL_PROJ!=0
AND PERSON = @SALESMAN) A
LEFT JOIN
(SELECT MARGIN, RATE
FROM COMMMARGINRATES) B
ON (ROUND(((A.TOTAL_PROJ-A.TOTALCOST)/A.TOTAL_PROJ*100),0)=B.MA RGIN)
ORDER BY A.PERSON, YEAR(A.DATECOMPL) DESC, MONTH(A.DATECOMPL) DESC
IF @ACTION='NOTPAIDREPORT'OR @ACTION='POSTCOMMISSIONS' OR
@ACTION='SUMNOTPAID'
UPDATE #TEMPCOMM
SET TOTALPAID=BASEPAID
WHERE MARGIN<40 AND TOTAL_PROJ<2000
AND RIGHT(NUMBER,LEN(NUMBER)-6)NOT LIKE 'SC%'
IF @ACTION='NOTPAIDREPORT'OR @ACTION='POSTCOMMISSIONS' OR
@ACTION='SUMNOTPAID'
UPDATE #TEMPCOMM
SET MARGINPAID=0
WHERE MARGIN<40 AND TOTAL_PROJ<2000
AND RIGHT(NUMBER,LEN(NUMBER)-6)NOT LIKE 'SC%'
-- MONITORING
IF @ACTION='NOTPAIDREPORT'OR @ACTION='POSTCOMMISSIONS' OR
@ACTION='SUMNOTPAID'
INSERT INTO #TEMPCOMM (PERSON, NUMBER, DESCRIPTIO, PDMONTH, PDYEAR,
TOTAL_PROJ)
SELECT PERSON, NUMBER, DESCRIPTIO, MONTH(DATEIN) AS PDMONTH,
YEAR(DATEIN) AS PDYEAR, TOTAL_PROJ
FROM PROJMASTSQL
WHERE STATUS IN ('G')
AND TOTAL_PROJ!=0
AND PERSON=@SALESMAN
AND RIGHT(NUMBER,LEN(NUMBER)-6) NOT IN ('IV', 'FI', 'UI')
AND NUMBER NOT IN (SELECT NUMBER FROM COMMLOG)
IF @ACTION='NOTPAIDREPORT'OR @ACTION='POSTCOMMISSIONS' OR
@ACTION='SUMNOTPAID'
UPDATE #TEMPCOMM
SET BASEPAID=50
WHERE RATE IS NULL
IF @ACTION='NOTPAIDREPORT'OR @ACTION='POSTCOMMISSIONS' OR
@ACTION='SUMNOTPAID'
UPDATE #TEMPCOMM
SET TOTALPAID=50
WHERE RATE IS NULL
-- INSPECTIONS
IF @ACTION='NOTPAIDREPORT'OR @ACTION='POSTCOMMISSIONS' OR
@ACTION='SUMNOTPAID'
INSERT INTO #TEMPCOMM (PERSON, NUMBER, DESCRIPTIO, PDMONTH, PDYEAR,
TOTAL_PROJ)
SELECT PERSON, NUMBER, DESCRIPTIO, MONTH(DATEIN) AS PDMONTH,
YEAR(DATEIN) AS PDYEAR, TOTAL_PROJ
FROM PROJMASTSQL
WHERE STATUS IN ('G')
AND TOTAL_PROJ!=0
AND PERSON=@SALESMAN
AND RIGHT(NUMBER,LEN(NUMBER)-6)='IV'
AND NUMBER NOT IN (SELECT NUMBER FROM COMMLOG)
IF @ACTION='NOTPAIDREPORT'OR @ACTION='POSTCOMMISSIONS' OR
@ACTION='SUMNOTPAID'
UPDATE #TEMPCOMM
SET BASEPAID=(TOTAL_PROJ*.07)
WHERE RATE IS NULL
AND BASEPAID IS NULL
AND RIGHT(NUMBER,LEN(NUMBER)-6)='IV'
IF @ACTION='NOTPAIDREPORT'OR @ACTION='POSTCOMMISSIONS' OR
@ACTION='SUMNOTPAID'
UPDATE #TEMPCOMM
SET TOTALPAID=(TOTAL_PROJ*.07)
WHERE RATE IS NULL
AND TOTALPAID IS NULL
AND RIGHT(NUMBER,LEN(NUMBER)-6)='IV'
-- SUB CONTRACT LABOR
IF @ACTION='NOTPAIDREPORT'OR @ACTION='POSTCOMMISSIONS' OR
@ACTION='SUMNOTPAID'
INSERT INTO #TEMPCOMM (PERSON, NUMBER, DESCRIPTIO, PDMONTH, PDYEAR,
TOTAL_PROJ, MARGIN)
SELECT PERSON, NUMBER, DESCRIPTIO, MONTH(DATEIN) AS PDMONTH,
YEAR(DATEIN) AS PDYEAR, TOTAL_PROJ,
((TOTAL_PROJ-COALESCE(TOTALCOST,0))/TOTAL_PROJ*100) AS MARGIN
FROM PROJMASTSQL
WHERE STATUS IN ('D')
AND PERSON=@SALESMAN
AND TOTAL_PROJ!=0
AND RIGHT(NUMBER,LEN(NUMBER)-6) LIKE 'SC%'
AND NUMBER NOT IN (SELECT NUMBER FROM COMMLOG)
IF @ACTION='NOTPAIDREPORT'OR @ACTION='POSTCOMMISSIONS' OR
@ACTION='SUMNOTPAID'
UPDATE #TEMPCOMM
SET TOTALPAID=(TOTAL_PROJ*.01)
WHERE RATE IS NULL
AND TOTALPAID IS NULL
AND RIGHT(NUMBER,LEN(NUMBER)-6) LIKE 'SC%'
AND MARGIN>16.5
IF @ACTION='NOTPAIDREPORT'OR @ACTION='POSTCOMMISSIONS' OR
@ACTION='SUMNOTPAID'
UPDATE #TEMPCOMM
SET BASEPAID=(TOTAL_PROJ*.01)
WHERE RATE IS NULL
AND RIGHT(NUMBER,LEN(NUMBER)-6) LIKE 'SC%'
AND MARGIN>16.5
-- PULL REPORT DATA
IF @ACTION='NOTPAIDREPORT'
SELECT LTRIM(NUMBER) AS NUMBER, PERSON, DESCRIPTIO, PDMONTH, PDYEAR,
TOTAL_PROJ, MARGIN, RATE, BASEPAID, MARGINPAID, TOTALPAID
FROM #TEMPCOMM
IF @ACTION='NOTPAIDREPORT'
DROP TABLE #TEMPCOMM
-- POST COMMISSIONS
IF @ACTION='POSTCOMMISSIONS'
INSERT INTO COMMLOG (PERSON, NUMBER, DESCRIPTIO, TOTAL_PROJ, MARGIN,
RATE, BASEPAID, MARGINPAID, TOTALPAID, PAIDDATE)
SELECT A.PERSON, A.NUMBER, A.DESCRIPTIO, A.TOTAL_PROJ, A.MARGIN,
A.RATE, A.BASEPAID, A.MARGINPAID, A.TOTALPAID, GETDATE() AS PAIDDATE
FROM #TEMPCOMM A
IF @ACTION='POSTCOMMISSIONS'
SELECT PERSON, NUMBER, DESCRIPTIO, MONTH(DATECOMPL) AS PDMONTH,
YEAR(DATECOMPL) AS PDYEAR, TOTAL_PROJ,
MARGIN, RATE, BASEPAID, MARGINPAID, TOTALPAID
FROM COMMLOG
WHERE PERSON=@SALESMAN AND
TOTALPAID!=0 AND
MONTH(PAIDDATE)=MONTH(GETDATE()) AND
YEAR(PAIDDATE)=YEAR(GETDATE()) AND
DAY(PAIDDATE)=DAY(GETDATE())
ORDER BY YEAR(DATECOMPL) DESC, MONTH(DATECOMPL) DESC
IF @ACTION='POSTCOMMISSIONS'
DROP TABLE #TEMPCOMM
-- SUM NOT PAID
IF @ACTION='SUMNOTPAID'
SELECT COALESCE(SUM(TOTALPAID),0) AS SUMNOTPAID
FROM #TEMPCOMM
WHERE PERSON=@SALESMAN
IF @ACTION='SUMNOTPAID'
DROP TABLE #TEMPCOMM
The ASP PAGE IS:
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master"
AutoEventWireup="false" CodeFile="COMMISSIONS.aspx.vb"
Inherits="Default2" title="Commissions Report" %>
Runat="Server">
Commission Report
Salesman:
DataValueField="PERSON">
Instructions:
Currently only the Not Paid Report is
functional check back soon for the historical
paid report.
Report Type:
asp:ListItem>
Please Wait...
CellPadding="2" DataSourceID="SUMNOTPAID"
ForeColor="Black">
Total:
BorderStyle="None" BorderWidth="1px" CellPadding="4"
DataSourceID="COMMISSIONS"
ForeColor="Black" GridLines="Vertical"
AutoGenerateColumns="False" Font-Size="X-Small" Width="860px"
AllowSorting="True">
PROJECTDETAIL.ASPX?PROJECT={0}" SortExpression="NUMBER" />
DataFormatString="{0:C}" />
DataFormatString="{0:#.##}" />
>
DataFormatString="{0:c}" />
DataFormatString="{0:c}" />
DataFormatString="{0:c}" />
SelectCommand="SP_COMMISSIONS"
SelectCommandType="StoredProcedure">
Type="String" />
Type="String" DefaultValue="" />
SelectCommand="SP_COMMISSIONS_SUMS"
SelectCommandType="StoredProcedure">
Type="String" />
SelectCommand="SELECT DISTINCT(PERSON) AS PERSON
FROM
PROJMASTSQL
ORDER BY PERSON">