Skip to content

String was not recognized as a valid DateTime.why?

Akshaya arumugam

Asked Dec 6, 2017, 6:23 AM EST1 answer
Share
LinkedInX
while i am trying to fetch datetime data from db it shows the error  String was not recognized as a valid DateTime.
 
 
code:
 
Model: 
 public DateTime? ReviewedDate{get;set;}
 
Controller: 
DateTime? ReviewedDate = null;
if (testordervm.ReviewedDate != DateTime.MinValue)
Code
                {
                    ReviewedDate = DateTime.Parse(dsobj.Tables[0].Rows[0]["ReviewedDate"].ToString());
                }
                else
                {
                    ReviewedDate = null;
                }
 

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Željko Perić
    0
    The DATE value that You are fetching from data base is not in valid DateTime format,
    so the DateTime.Parse method returns error message.
     
    Try to use :
    1. MessageBox.Show(dsobj.Tables[0].Rows[0]["ReviewedDate"].ToString()); 
     to check what are You trying to parse to date and time.
     
    Perhaps the value returned from db is null, or the value is not in correct DateTime format,
    that could be changed by formatting string to recognizable date time format:
     
    1. ReviewedDate = DateTime.Parse(dsobj.Tables[0].Rows[0]["ReviewedDate"].ToString("dd.MM.yyyy)); 
     
    Here is url where You can learn all about "Standard Date and Time Format Strings" and Custom formatting.
     
     
    All the best,
    Željko Peric
    0