System.FormatException: String was not recognized as a valid DateTime

Hello Experts,

DataType in my database is DateTime. But when I try to pass the date from my model,
I am getting the above mentioned error.

There are chances of the string being null sometimes please help.

int newTaskID;
                saved = TaskDAL.Insert(task.AssignedTo.UserID, task.Subject, task.Description, Convert.ToDateTime(task.DateTime), task.NoDateTime, task.Location.LocationGroupID, task.Priority, task.Status, task.Remind, task.Minutes, task.Email, out newTaskID);
                 task.TaskID = newTaskID;
rowmarkAsked:
Who is Participating?
 
sandip132Connect With a Mentor Commented:
TRY THIS...C# Code

System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-US");  // Set your required format culture
DateTime a = DateTime.Parse(task.DateTime,culture,System.Globalization.DateTimeStyles.NoCurrentDateDefault);

Regards,
Sandip.
0
 
craskinCommented:
try using CDate() instead of Convert.ToDateTime()
0
 
rowmarkAuthor Commented:
No CDate() doesnt work.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
XeavnCommented:
What exactly is being passed to it? In other words what is the value of your string at this point?
0
 
rowmarkAuthor Commented:
25/10/2006
0
 
Roopesh_7Commented:
datetime dt ;

if(task.DateTime != null)
     dt =  Convert.ToDateTime(task.DateTime);
else
      dt = datetime.\\; give defult value = 01/01/1900

then pass dt to function

             
0
 
rowmarkAuthor Commented:
nope this doesnt work as my database date format is  mm/dd/yyyy

0
All Courses

From novice to tech pro — start learning today.