Hi
On below line i am getting error - Operator < cannot be applied to operands of type String and DateTime?
var SessionDate = ((Literal)e.Item.FindControl("ltrlLastSessionDate")).Text;
if (Convert.ToDateTime(SessionDate).ToString("dd-MM-yyyy") < objGrpResult.GrpSessionStartDate)
Amit MohantyPosted Apr 19, 2023, 8:57 AM
The error message you are receiving is because you are trying to compare a string representation of a date with a
DateTimeobject.The
Convert.ToDateTime(SessionDate)method call returns aDateTimeobject, but when you callToString("dd-MM-yyyy")on it, you are converting it back to a string. The comparison operation<expects twoDateTimeobjects as operands, but you are providing a string and aDateTimeobject.To fix this error, you should convert
objGrpResult.GrpSessionStartDateto a string in the same format asSessionDatebefore performing the comparison. You can do this using theToString("dd-MM-yyyy")method.Here's the corrected code:
Jignesh KumarPosted Apr 18, 2023, 10:10 AM
Hello Ramco,
You are trying to compare string with datetime value, Both are not compatible, both value should be datetime values then only you can compare.
Try this one,
Naimish MakwanaPosted Apr 18, 2023, 10:09 AM
The error message is indicating that you are trying to compare a string (
Convert.ToDateTime(SessionDate).ToString("dd-MM-yyyy")) and aDateTimeobject (objGrpResult.GrpSessionStartDate) using the<operator, which is not allowed.To fix this error, you can convert
objGrpResult.GrpSessionStartDateto a string using the same date format before comparing it toSessionDate. Here's an updated version of your code:Thanks