Link to home
Start Free TrialLog in
Avatar of Michael Sterling
Michael SterlingFlag for United States of America

asked on

What is creating a newline in my constant?

I'm getting a "newline in constant" error in my C# after adding the TIME_FORMAT function(from MySQL), into an SQL string that I'm building and I'm not sure why or how to fix it. I tried to escape the '%' character that precedes the 'T' for the 2nd parameter of the function, but it didn't solve the problem and gave me a different error as a matter of fact. Why does it think that by adding "TIME_FORMAT("00:00:00", "%T")  to my string, that I'm adding a new line in my constant? Please help.


my MySQL string in C#
sql = String.Format(@"SELECT 
	AthleteUid, AthleteFullName, SEC_TO_TIME((SUM(TIMESTAMPDIFF(MINUTE, TimeIn, TimeOut))) * 60) AS WeekTotal, RequiredHours, SportTeamName AS SportTeam 
FROM 
	utathleteinfo uai 
    	INNER JOIN 
    utinout uio on uai.AthleteUid = uio.StudentID 
    	INNER JOIN 
    utsportteam ust ON ust.utSportTeamUid = uai.SportUid 
WHERE 
	uio.TimeOut IS NOT NULL 
    AND uio.TimeIn >= '2018-01-29 00:00:00' 
    AND uio.TimeOut <= '2018-02-02 23:59:59' 
GROUP BY 
	uai.AthleteUid, AthleteFullName, WeekTotal, RequiredHours, SportTeamName, SemesterTime, Coach 
ORDER BY 
 	AthleteFullName)
UNION
(SELECT 
	AthleteUid, AthleteFullName, TIME_FORMAT("00:00:00", "%T") AS WeekTotal, RequiredHours, SportTeamName AS SportTeam 
FROM 
	utathleteinfo uai 
    	INNER JOIN 
    utinout uio on uai.AthleteUid = uio.StudentID 
    	INNER JOIN 
    utsportteam ust ON ust.utSportTeamUid = uai.SportUid 
WHERE 
	WeekTotal = '0:00'
GROUP BY 
	uai.AthleteUid, AthleteFullName, WeekTotal, RequiredHours, SportTeamName, SemesterTime, Coach
ORDER BY 
 	AthleteFullName) ORDER BY AthleteFullName");

Open in new window


the error:

" Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1010: Newline in constant

Source Error:


Line 202:                              sql = sql + String.Format(@" UNION
Line 203:                                    (SELECT
Line 204:                                          AthleteUid, AthleteFullName, TIME_FORMAT("00:00:00", "%T") AS WeekTotal, RequiredHours, SportTeamName AS SportTeam
Line 205:                                    FROM
Line 206:                                          utathleteinfo uai "
ASKER CERTIFIED SOLUTION
Avatar of Ares Kurklu
Ares Kurklu
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Michael Sterling

ASKER

That worked. Thanks.
Avatar of Norie
Norie

Have you tried using single quotes instead of double quotes inside the SQL statement?
...
SELECT 
	AthleteUid, AthleteFullName, TIME_FORMAT('00:00:00', '%T') AS WeekTotal, RequiredHours, SportTeamName AS SportTeam 
FROM 
	utathleteinfo uai 
    	INNER JOIN 
    utinout uio on uai.AthleteUid = uio.StudentID 
    	INNER JOIN 
    utsportteam ust ON ust.utSportTeamUid = uai.SportUid 
...

Open in new window

@Norie: That was my first attempt and it didn't work. The double quotes solved it.
Strange, you use single quotes in other parts of the SQL statement and when I tested it (lightly) it seemed to work fine.:)
Yeah, the whole thing threw me off. I didn't understand why it didn't work in the first place.