• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1008
  • Last Modified:

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
0
stankstank
Asked:
stankstank
3 Solutions
 
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
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now