tags:

views:

489

answers:

2

I am trying to save data into my database using a vb form. I am using a datetimepicker to insert the date into my database. Here's an example

saveCommand.Parameters.AddWithValue("A_BOOKDATE", abookdatePicker1.Text).DbType = DbType.DateTime

In my database i set its attribute to time ,now the thing is i used the same line of code on another database and it worked but this one is giving this error 'Failed to convert parameter value from string to datetime'

How do i convert this string to datetime. Many Thanks

+4  A: 

Use DateTimePicker.Value instead of DateTimePicker.Text:

saveCommand.Parameters.AddWithValue("A_BOOKDATE", abookdatePicker1.Value) _
                      .DbType = DbType.DateTime

(If you ever actually have to convert a string to a DateTime, use DateTime.TryParseExact or DateTime.ParseExact, but in this case it's better not to use the string representation at all.)

Jon Skeet
cheers. I tried it and it works.
A: 

You could also try to use

Convert.ToDateTime Method (String) (this one will return a datatime object or throw and exception)

DateTime.TryParse Method (String, DateTime) (this one returns the datetime as an output parameter, returns null on failure)

Keivan
thanks i did use this one and it worked even with the .text.