I have a query where I try to fetch the sum value but I get the InvalidCastException.
My query is:
SELECT e.clockNr,e.firstName,e.LastName,e.unionName,i.points
FROM (
SELECT employee.clockNr AS clockNr,
employee.firstName AS firstName,
employee.lastName AS lastName,
Unions.name AS unionName
FROM employee,Unions
WHERE employee.active=1 AND employee.unionId = unions.id
GROUP BY employee.clockNr
) e LEFT JOIN (
SELECT infraction.clockNr AS clockNr,
CAST(SUM(Infraction.points) AS SIGNED) AS points
FROM infraction
WHERE infraction.infractionDate >=@startDate
AND infraction.infractionDate <=@endDate
GROUP BY infraction.clockNr
) i ON e.clockNr = i.clockNr
ORDER BY e.clockNr ASC
It is the 'points' column where it goes wrong. I have added the CAST to SIGNED but this is not helping.
The way I read out the column is:
int iGetPoints = Convert.ToInt32(reportReader["points"]);
Also tried:
int iGetPoints = (int)reportReader["points"];
But both raise the InvalidCastException. The query is tested in PHPMyAdmin and working fine there.
Can anyone see what I am doing wrong or give me a hint where to look for?
pointscolumn