Solved

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

Posted on 2006-10-25
7
299 Views
Last Modified: 2008-01-09
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;
0
Comment
Question by:rowmark
7 Comments
 
LVL 12

Expert Comment

by:craskin
ID: 17807428
try using CDate() instead of Convert.ToDateTime()
0
 

Author Comment

by:rowmark
ID: 17807527
No CDate() doesnt work.
0
 
LVL 4

Expert Comment

by:Xeavn
ID: 17807759
What exactly is being passed to it? In other words what is the value of your string at this point?
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:rowmark
ID: 17808119
25/10/2006
0
 
LVL 6

Expert Comment

by:Roopesh_7
ID: 17808494
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
 

Author Comment

by:rowmark
ID: 17808654
nope this doesnt work as my database date format is  mm/dd/yyyy

0
 
LVL 12

Accepted Solution

by:
sandip132 earned 500 total points
ID: 17809008
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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now