BASEER ULHASSAN BASEER ULHASSAN - 1 month ago 11
C# Question

Concat string with a Query with DateTime String

I am unable to Add CurrentSystemTime to my Query manually. The Query is stored in a string variable. But the concationation part is giving error:

The Query is:

string myQuery= @"SELECT MAX(ASD.eventDateTime) AS second, R.resourceID,R.resourceLoginID,ASD.agentID
FROM AgentStateDetail AS ASD INNER JOIN
Resource AS R ON ASD.agentID = R.ResourceID WHERE ASD.eventDateTime >='" + DateTime.Today.ToString("yyyy-MM-dd h:mm:ss") + "'AND ASD.eventDateTime <='2016-10-18 23:59:59'
GROUP BY R.ResourceID,R.resourceLoginID,ASD.agentID"



The Problem part is:

WHERE ASD.eventDateTime >='" +
DateTime.Today.ToString("yyyy-MM-dd h:mm:ss") + "'

Error is:

Represent Text as unicode character Newline in constant.

Answer

You forgot to start the second part of the string with an @ which is why the compiler amends that the newline character is invalid:

string myQuery= @"SELECT MAX(ASD.eventDateTime) AS second, R.resourceID,R.resourceLoginID,ASD.agentID
                  FROM AgentStateDetail AS ASD INNER JOIN 
                  Resource AS R ON ASD.agentID = R.ResourceID WHERE ASD.eventDateTime >='" + DateTime.Today.ToString("yyyy-MM-dd h:mm:ss") + @"'AND ASD.eventDateTime <='2016-10-18 23:59:59' 
                  GROUP BY R.ResourceID,R.resourceLoginID,ASD.agentID"