Datetime conversion issue from Linq

Binumon George 161 Reputation points
2023-04-22T17:29:14.73+00:00

I've included some code from my application that uses Linq to retrieve data from a database.



As previously stated, the code stores data in the var variable. This var variable holds a datetime value. However, while using the following code, the following error occurs. Code : DateTime LastNotificationDate = DateTime.Parse(LastNotificationRecord.Value); Error : String was not recognized as a valid In database data stored as : 4/22/2023 11:00:00 AM NB: Not able to change dateformat in database. How i overcome this issue

.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,103 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,549 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 122.2K Reputation points
    2023-04-22T18:11:19.2733333+00:00

    In case of strings, check an example:

    DateTime LastNotificationDate = DateTime.Parse( "4/22/2023 11:00:00 AM", CultureInfo.InvariantCulture );
    

    However, the values should be stored using date and time types (datetime or datetime2, for example). LastNotificationRecord.Value should be a DateTime. Parsing should be avoided.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.