String was not recognized as a valid DateTime.

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");

This:
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.

Stankstank
stankstankAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

stankstankAuthor Commented:
I think this is because of the SqlDataSource control.
0
aki4uCommented:
You can not asssign DateTime object to Value.
You must use .ToString() ...like this

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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
existenz2Commented:
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.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

YZlatCommented:
try

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

or

DateTime myDate=new DateTime();
  MyDate= DateTime.Parse("9/4/2001");
0
YZlatCommented:
or

DateTime MyDate = Convert.ToDateTime("9/4/2001 9:54 PM");
e.Command.Parameters["@Start_Time"].Value=MyDate
0
stankstankAuthor Commented:
Thanks guys.  with all of your advice and my powerful superpowers, I put the stank on it.  It works!

STANKSTANK
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.