Solved

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

Posted on 2006-10-25
7
303 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

862 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

24 Experts available now in Live!

Get 1:1 Help Now