String was not recognized as a valid DateTime.

Posted on 2006-04-01
Last Modified: 2012-05-05
I am using a SqlDataSource object to insert records into a database.  I get the error "String was not recognized as a valid DateTime." when I try to pass a datetime variable to the stored procedure.  I have limited the problem to my code - it doesn't even make it to the stored procedure, so that's not a problem.

This code is in the Inserting event of the SqlDataSource object:

I have tried this:
e.Command.Parameters["@Start_Time"].Value = Convert.ToDateTime("9/4/2001");

e.Command.Parameters["@Start_Time"].Value = Convert.ToDateTime("9/4/2001 9:54 PM");

and a whole bunch of other combinations, even using DateTime.Parse.  This is very awkward because it only has a problem with the Insert method of the SqlDataSource object - again, it doesn't even call the stored procedure at this time, so it isn't a problem with the stored procedure.  It errors out before it can even do this!  

Converting to datetime should be the easiest task in.NET - have you had a similar problem?  Please help!  I can't figure it out.

Question by:stankstank

    Author Comment

    I think this is because of the SqlDataSource control.
    LVL 10

    Accepted Solution

    You can not asssign DateTime object to Value.
    You must use .ToString() this

    e.Command.Parameters["@Start_Time"].Value = Convert.ToDateTime("9/4/2001").ToString();

    LVL 14

    Assisted Solution

    The problem might not be your code, but your SQL Server, which dislikes the format in which the datetime gets inserted. This is a pretty common error. Have a look in your profiler and Query Analyzer to validate the queries.
    LVL 35

    Assisted Solution


    e.Command.Parameters["@Start_Time"].Value = CDate("9/4/2001");


    DateTime myDate=new DateTime();
      MyDate= DateTime.Parse("9/4/2001");
    LVL 35

    Expert Comment


    DateTime MyDate = Convert.ToDateTime("9/4/2001 9:54 PM");

    Author Comment

    Thanks guys.  with all of your advice and my powerful superpowers, I put the stank on it.  It works!


    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    I have developed many web applications with asp & and to add and use a dropdownlist was always a very simple task, but with the new, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    734 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

    22 Experts available now in Live!

    Get 1:1 Help Now