C
C#2y ago
Wisch10

✅ Convert null datetime error in sqlite myReader

anyone know how to fix an issue with pulling a null datetime from sqlite into myReader? it keeps throwing an error that '' is an invalid string. I even put in a case like (myReader["columnName"].ToString() != null) ? Convert.ToDateTime(myReader["columnName"].ToString()) : Convert.ToDateTime(null) and that does not work
11 Replies
Jimmacle
Jimmacle2y ago
it won't necessarily fix your error, but you should use DateTime.Parse instead of Convert.ToDateTime most of the conversion methods in Convert are obsolete besides that, debug your code to check the string you're getting before trying to parse it
Ang619
Ang6192y ago
How about: if (myReader["columnName"] != DBNull.Value)
Jimmacle
Jimmacle2y ago
potentially, i'm not too familiar with raw ADO.NET
Wisch10
Wisch10OP2y ago
Wisch10
Wisch10OP2y ago
it doesn't like the DateTime.Parse either... keeps telling me that '' is an invalid string
Jimmacle
Jimmacle2y ago
correct, if the string is empty then it's not a valid DateTime string
Wisch10
Wisch10OP2y ago
the DBNull.Value did work though... not sure why that would work vs just null
Jimmacle
Jimmacle2y ago
probably an ADO.NET specific thing is there a reason you aren't using an ORM?
Wisch10
Wisch10OP2y ago
ORM?
Jimmacle
Jimmacle2y ago
object relational mapper, basically a library that can handle anything from basic data mapping to managing your whole DB and generating queries from C# code the former would be something like Dapper, latter like EF Core
Wisch10
Wisch10OP2y ago
I am moving to EFCore but wanted to get the basic program working before I change it all over because then I can change it by class without ruining the whole thing
Want results from more Discord servers?
Add your server