0

I do not understand where I'm making the mistake that gives me this error System.FormatException: 'String was not recognized as a valid DateTime'

UserDOB work correctly, but DateTimeStamp not.

while (reader.Read())
{
    list1.Add(new User()
    {
        UserId = Convert.ToInt32(reader["UserId"]),
        UserName = reader["UserName"].ToString(),
        UserSurname = reader["UserSurname"].ToString(),
        UserPassportNo = reader["UserPassportNo"].ToString(),
        UserAccessPoint = reader["UserAccessPoint"].ToString(),
        UserAccessType = reader["UserAccessType"].ToString(),
        UserDOB = DateTime.Parse(reader["UserDOB"].ToString()),
        DateTimeStamp = DateTime.Parse(reader["DateTimeStamp"].ToString()),
    });
}

My model.DatabaseConnection

public class User
{
    public int UserId { get; set; }
    public string UserName { get; set; }
    public string UserSurname { get; set; }
    public string UserPassportNo { get; set; }
    public string UserAccessPoint { get; set; }
    public string UserAccessType { get; set; }
    public System.DateTime UserDOB { get; set; }

    //[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
    public System.DateTime DateTimeStamp { get; set; }
}
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
  • most likely the data is null. – BugFinder Jul 26 '18 at 11:13
  • what does the string look like? can you provide an example? – mast3rd3mon Jul 26 '18 at 11:14
  • 5
    Unless you give us the string you're trying to parse, there's nothing we can do to help. – DavidG Jul 26 '18 at 11:16
  • 8
    Don't use `ToString` and then force your code to convert a string *back* into a `DateTime`. Use `DateTime` datatypes throughout your code. Converting everything into strings *introduces* the opportunity for problems – Damien_The_Unbeliever Jul 26 '18 at 11:16
  • 2
    1. Check if the value is `System.DBNull.Value`, you can use [reader.IsDBNull](https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.isdbnull(v=vs.110).aspx) 2) Hopefully the table schema's column type for `UserDOB` is `Date` or `DateTime` or `DateTime2` in which case you should use [reader.GetDateTime](https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getdatetime(v=vs.110).aspx). This is assuming it is Sql Server you are querying. – Igor Jul 26 '18 at 11:16
  • use reader["DateTimeStamp"].ToString() as reader["DateTimeStamp"]?.ToString() this might resolve your problem and type of your DateTimeStamp should be DataTime? – Amit Bisht Jul 26 '18 at 11:16
  • possible duplicate of this (https://stackoverflow.com/questions/2193012/string-was-not-recognized-as-a-valid-datetime-format-dd-mm-yyyy) – Bharath_Developer Jul 26 '18 at 11:23

1 Answers1

3

It seems that you store date as a string in DB. This varchar doesn´t meet format to parse it to DateTime. Change your SQL query to DB so it returns your DateTimeStamp as a correct format (yyyy-MM-dd) or convert it to DateTime with SQL before you read it from code.

Also when you use DataReader try to look at reader.GetDateTime(reader.getordinal("DateTimeStamp")) function.

Somachr
  • 464
  • 1
  • 8
  • 21