0

I'm trying to insert in my database (SQL Server 2012) a date (dd/MM/yyyy) from a DateTimePicker. When I checked my database, the format has been saved like (yyyy/dd/MM), the problem is when I choose a day between 13 and the last day of the month, I got the message "error converting data type varchar to datetime". But if I choose a day between 01 and 12, it works.

My class is something like this:

public class Example
{
   private SqlDateTime date;

   public SqlDateTime date
   {
      get {return date;}
      set {date = value;}
   }
}

And the object that is getting my date is:

  Example example = new Example();
  example.date = SqlDatime.Parse(dateTimePicker.Value.Date);

My query just takes the (example.date) and insert it in the database. Any idea?

Thanks in advance, and sorry about my english

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Anderson Gama
  • 77
  • 1
  • 8

2 Answers2

0

At least when I had this problem, I solved by formatting the date as yyyy-MM-dd and it solved for a Datetime field in SQL. Try this :

Example example = new Example();
example.date = dateTimePicker.Value.Date.ToString("yyyy-MM-dd");
André Silva
  • 1,149
  • 9
  • 30
0

Use : dateTimePicker.Value.Date.ToString("yyyy-MM-dd HH:mm:ss");

Sahil Sareen
  • 1,813
  • 3
  • 25
  • 40