Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Sql Datetime

Sql Datetime

Scheduled Pinned Locked Moved C#
databasehelpquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JelleM
    wrote on last edited by
    #1

    Hello, I am using the following piece of code to save data to my database: SqlDateTime date = DateTime.Now; SqlCommand myCommand2 = new SqlCommand("INSERT INTO orders (date,customerid,productid,sum) VALUES ('" + date + "'," + index + ",'" + productid + "','" + aantal + "')", connect); myCommand2.ExecuteScalar(); the date-field in the database has the property datetime. When i try to run the query, he always gives following error: The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.rnThe statement has been terminated. I tried to use DateTime.Now directly in the query, but it still gives the same error, also writing a date by hand (in the values-field in the query) gives an error. When I write a date (exactly the same format as the one above) directly in the database (without a query), i have no errors what so ever. Can somebody help? Thanks!

    A M 2 Replies Last reply
    0
    • J JelleM

      Hello, I am using the following piece of code to save data to my database: SqlDateTime date = DateTime.Now; SqlCommand myCommand2 = new SqlCommand("INSERT INTO orders (date,customerid,productid,sum) VALUES ('" + date + "'," + index + ",'" + productid + "','" + aantal + "')", connect); myCommand2.ExecuteScalar(); the date-field in the database has the property datetime. When i try to run the query, he always gives following error: The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.rnThe statement has been terminated. I tried to use DateTime.Now directly in the query, but it still gives the same error, also writing a date by hand (in the values-field in the query) gives an error. When I write a date (exactly the same format as the one above) directly in the database (without a query), i have no errors what so ever. Can somebody help? Thanks!

      A Offline
      A Offline
      Arjan Einbu
      wrote on last edited by
      #2

      Have you had a look at at the actual SQL string produced by your string concatenation? I'm guessing there's a format mismatch, maybe due to some regional settings or something. (Often a problem when putting datetimes into the db like this.) A safe and easy way to fix this would be to use parameters in your SQL query:

      string query = @"
      INSERT INTO orders (date,customerid,productid,sum)
      VALUES (@date, @customerid, @sum)
      ";
      SqlCommand cmd = new SqlCommand(query, con);
      cmd.Parameters.Add("@date", DateTime.Now);
      cmd.Parameters.Add("@customerid", customerid);
      cmd.Parameters.Add("@sum", sum);
      cmd.ExecuteNonScalar();

      J 1 Reply Last reply
      0
      • J JelleM

        Hello, I am using the following piece of code to save data to my database: SqlDateTime date = DateTime.Now; SqlCommand myCommand2 = new SqlCommand("INSERT INTO orders (date,customerid,productid,sum) VALUES ('" + date + "'," + index + ",'" + productid + "','" + aantal + "')", connect); myCommand2.ExecuteScalar(); the date-field in the database has the property datetime. When i try to run the query, he always gives following error: The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.rnThe statement has been terminated. I tried to use DateTime.Now directly in the query, but it still gives the same error, also writing a date by hand (in the values-field in the query) gives an error. When I write a date (exactly the same format as the one above) directly in the database (without a query), i have no errors what so ever. Can somebody help? Thanks!

        M Offline
        M Offline
        Michael Potter
        wrote on last edited by
        #3

        SqlServer likes to see a char dates in a specfic format. You will have to experiment a little but from memory it likes: 01/01/98 OR 01/01/98 23:59:59.999 It is not very tolerant with other formats. Try date.ToString("dd/MM/yyyy HH:mm:ss.000") in place of date BTW: I normally let the SQL Server set a DateTime stamp by using GetDate() in place of '" + date + "'. It will usually have a more accruate time then a workstation (at least it will be a common clock). There is also GetUTCDate() for when your users are in different time zones.

        L 1 Reply Last reply
        0
        • A Arjan Einbu

          Have you had a look at at the actual SQL string produced by your string concatenation? I'm guessing there's a format mismatch, maybe due to some regional settings or something. (Often a problem when putting datetimes into the db like this.) A safe and easy way to fix this would be to use parameters in your SQL query:

          string query = @"
          INSERT INTO orders (date,customerid,productid,sum)
          VALUES (@date, @customerid, @sum)
          ";
          SqlCommand cmd = new SqlCommand(query, con);
          cmd.Parameters.Add("@date", DateTime.Now);
          cmd.Parameters.Add("@customerid", customerid);
          cmd.Parameters.Add("@sum", sum);
          cmd.ExecuteNonScalar();

          J Offline
          J Offline
          JelleM
          wrote on last edited by
          #4

          Thanks, that solved my problem!

          1 Reply Last reply
          0
          • M Michael Potter

            SqlServer likes to see a char dates in a specfic format. You will have to experiment a little but from memory it likes: 01/01/98 OR 01/01/98 23:59:59.999 It is not very tolerant with other formats. Try date.ToString("dd/MM/yyyy HH:mm:ss.000") in place of date BTW: I normally let the SQL Server set a DateTime stamp by using GetDate() in place of '" + date + "'. It will usually have a more accruate time then a workstation (at least it will be a common clock). There is also GetUTCDate() for when your users are in different time zones.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Michael Potter wrote:

            Try date.ToString("dd/MM/yyyy HH:mm:ss.000") in place of date

            date.ToString("s"); works for me as well. This put a "T" between the date and the time, but that seems to be accepted by MySQL in my case.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups