0

I am running query in SQL Server and it's running fine and returning data:

 var record = DbContext.Attendances
                       .SqlQuery("select id, checktype, locationinfo, latitude, longitude, employeeid, 
                                         activityid, DateNTime at time zone 'utc' at time zone 'pakistan standard time' as datentime " +
                                 "from(select *, CONVERT(varchar, DateNTime at time zone 'utc' at time zone 'pakistan standard time', 101) as tb from dbo.Attendances where EmployeeId = 3) as t "+
                                 "where tb = '03/05/2022'");

Exact same query is not working when I run it in the ASP.NET MVC app using .SqlQuery function. It gave me an error:

The 'DateNTime' property on 'Attendance' could not be set to a 'System.DateTimeOffset' value. You must set this property to a non-null value of type 'System.DateTime'.

Although I set DateNTime non-nulable.

Here is my table:

enter image description here

I cannot change DateTime to DateTime? because then I will have to change the code in many places.

1
  • Your query looks odd, look at "DateNTime at time zone 'utc' at time zone 'pakistan standard time' as datentime ", also why are you using SELECT .... FROM(SELECT...), why not just SELECT * FROM Attendance. The error message says that you are trying to set DateNTime to a DateTimeOffset when it requires a datetime. Do you have an update somewhere? Commented Mar 6, 2022 at 19:03

1 Answer 1

1

Update Your model and use ? for

public DateTime? DateNTime { get; set; }
Sign up to request clarification or add additional context in comments.

2 Comments

Is there any other solution? Because I have to change a lot of code to do this. Thanks.
Well, sorry for the code changes but this is a must do in order to avoid having exceptions. unfortunately, sometimes code leading to exceptions needs to be changed. this is how it is. we can't change that fact by a dirty hack that won't be clear/helpfull and may lead to other issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.