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. Visual Studio
  4. DateTimePicker syntax error

DateTimePicker syntax error

Scheduled Pinned Locked Moved Visual Studio
databasesql-serversysadminhelpquestion
8 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.
  • M Offline
    M Offline
    moordoom
    wrote on last edited by
    #1

    I am trying to do an SQL Select Query by using the dates supplied by two DateTimePickers to export data from a SQL Server to an Excel Spreadsheet. When I run the app, I get "Line1: Incorrect syntax near 'Tuesday, November 12, 2013 AND Wednesday, November 13, 2013'. " The query is being run on an SQL 2000 server, and the DateTime Format on the server is MM/dd/yyyy hh:mm:ss tt in the record. I have tried doing a DateTimePickerFormat DateTimePicker1.Format = DateTimePickerFormat.Custom DateTimePicker1.CustomFormat = "MM/d/yyyy hh:mm:ss tt" DateTimePicker2.Format = DateTimePickerFormat.Custom DateTimePicker2.CustomFormat = "MM/d/yyyy hh:mm:ss tt" Which gives the same error in 11/12/2013 02:00:00 PM 11/13/2013 02:00:00 PM I have Tried DatePicker1.Format = DateTimePickerFormat.Custom DateTimePicker1.CustomFormat = "MM/d/yyyy" DateTimePicker2.Format = DateTimePickerFormat.Custom DateTimePicker2.CustomFormat = "MM/d/yyyy" Which gives the same error in 11/12/2013 AND 11/13/2013 Here is my current Select code... Dim dataAdapter As New SqlClient.SqlDataAdapter() Dim dataSet As New DataSet Dim command As New SqlClient.SqlCommand Dim datatableMain As New System.Data.DataTable() Dim connection As New SqlClient.SqlConnection DateTimePicker1.Format = DateTimePickerFormat.Custom DateTimePicker1.CustomFormat = "MM/d/yyyy" DateTimePicker2.Format = DateTimePickerFormat.Custom DateTimePicker2.CustomFormat = "MM/d/yyyy" Dim dt1 As String = DateTimePicker1.Text Dim dt2 As String = DateTimePicker2.Text connection.ConnectionString = "Data Source=SQL01_2000;database=App_DB;uid=sa;pwd=sa" command.Connection = connection command.CommandType = CommandType.Text command.CommandText = "Select * from LabelCheck WHERE [Date1] BETWEEN '" & dt1 & " AND " & dt2 & "'" dataAdapter.SelectCommand = command What am I missing?

    P B 2 Replies Last reply
    0
    • M moordoom

      I am trying to do an SQL Select Query by using the dates supplied by two DateTimePickers to export data from a SQL Server to an Excel Spreadsheet. When I run the app, I get "Line1: Incorrect syntax near 'Tuesday, November 12, 2013 AND Wednesday, November 13, 2013'. " The query is being run on an SQL 2000 server, and the DateTime Format on the server is MM/dd/yyyy hh:mm:ss tt in the record. I have tried doing a DateTimePickerFormat DateTimePicker1.Format = DateTimePickerFormat.Custom DateTimePicker1.CustomFormat = "MM/d/yyyy hh:mm:ss tt" DateTimePicker2.Format = DateTimePickerFormat.Custom DateTimePicker2.CustomFormat = "MM/d/yyyy hh:mm:ss tt" Which gives the same error in 11/12/2013 02:00:00 PM 11/13/2013 02:00:00 PM I have Tried DatePicker1.Format = DateTimePickerFormat.Custom DateTimePicker1.CustomFormat = "MM/d/yyyy" DateTimePicker2.Format = DateTimePickerFormat.Custom DateTimePicker2.CustomFormat = "MM/d/yyyy" Which gives the same error in 11/12/2013 AND 11/13/2013 Here is my current Select code... Dim dataAdapter As New SqlClient.SqlDataAdapter() Dim dataSet As New DataSet Dim command As New SqlClient.SqlCommand Dim datatableMain As New System.Data.DataTable() Dim connection As New SqlClient.SqlConnection DateTimePicker1.Format = DateTimePickerFormat.Custom DateTimePicker1.CustomFormat = "MM/d/yyyy" DateTimePicker2.Format = DateTimePickerFormat.Custom DateTimePicker2.CustomFormat = "MM/d/yyyy" Dim dt1 As String = DateTimePicker1.Text Dim dt2 As String = DateTimePicker2.Text connection.ConnectionString = "Data Source=SQL01_2000;database=App_DB;uid=sa;pwd=sa" command.Connection = connection command.CommandType = CommandType.Text command.CommandText = "Select * from LabelCheck WHERE [Date1] BETWEEN '" & dt1 & " AND " & dt2 & "'" dataAdapter.SelectCommand = command What am I missing?

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2
      1. Do not store dates as strings. 1) Use a parameterized query.
      M 1 Reply Last reply
      0
      • P PIEBALDconsult
        1. Do not store dates as strings. 1) Use a parameterized query.
        M Offline
        M Offline
        moordoom
        wrote on last edited by
        #3

        so, something like this then... connection.ConnectionString = "Data Source=SQL01_2000;database=App_DB;uid=sa;pwd=sa" command.Connection = connection command.CommandType = CommandType.Text command.CommandText = "Select * from LabelCheck WHERE Date1 BETWEEN (@date1) AND (@date2)" command.Parameters.Add(New SqlParameter("@date1", DateTimePicker1.Value.Date)) command.Parameters.Add(New SqlParameter("@date2", DateTimePicker2.Value.Date)) dataAdapter.SelectCommand = command

        P 1 Reply Last reply
        0
        • M moordoom

          so, something like this then... connection.ConnectionString = "Data Source=SQL01_2000;database=App_DB;uid=sa;pwd=sa" command.Connection = connection command.CommandType = CommandType.Text command.CommandText = "Select * from LabelCheck WHERE Date1 BETWEEN (@date1) AND (@date2)" command.Parameters.Add(New SqlParameter("@date1", DateTimePicker1.Value.Date)) command.Parameters.Add(New SqlParameter("@date2", DateTimePicker2.Value.Date)) dataAdapter.SelectCommand = command

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          Yes.

          1 Reply Last reply
          0
          • M moordoom

            I am trying to do an SQL Select Query by using the dates supplied by two DateTimePickers to export data from a SQL Server to an Excel Spreadsheet. When I run the app, I get "Line1: Incorrect syntax near 'Tuesday, November 12, 2013 AND Wednesday, November 13, 2013'. " The query is being run on an SQL 2000 server, and the DateTime Format on the server is MM/dd/yyyy hh:mm:ss tt in the record. I have tried doing a DateTimePickerFormat DateTimePicker1.Format = DateTimePickerFormat.Custom DateTimePicker1.CustomFormat = "MM/d/yyyy hh:mm:ss tt" DateTimePicker2.Format = DateTimePickerFormat.Custom DateTimePicker2.CustomFormat = "MM/d/yyyy hh:mm:ss tt" Which gives the same error in 11/12/2013 02:00:00 PM 11/13/2013 02:00:00 PM I have Tried DatePicker1.Format = DateTimePickerFormat.Custom DateTimePicker1.CustomFormat = "MM/d/yyyy" DateTimePicker2.Format = DateTimePickerFormat.Custom DateTimePicker2.CustomFormat = "MM/d/yyyy" Which gives the same error in 11/12/2013 AND 11/13/2013 Here is my current Select code... Dim dataAdapter As New SqlClient.SqlDataAdapter() Dim dataSet As New DataSet Dim command As New SqlClient.SqlCommand Dim datatableMain As New System.Data.DataTable() Dim connection As New SqlClient.SqlConnection DateTimePicker1.Format = DateTimePickerFormat.Custom DateTimePicker1.CustomFormat = "MM/d/yyyy" DateTimePicker2.Format = DateTimePickerFormat.Custom DateTimePicker2.CustomFormat = "MM/d/yyyy" Dim dt1 As String = DateTimePicker1.Text Dim dt2 As String = DateTimePicker2.Text connection.ConnectionString = "Data Source=SQL01_2000;database=App_DB;uid=sa;pwd=sa" command.Connection = connection command.CommandType = CommandType.Text command.CommandText = "Select * from LabelCheck WHERE [Date1] BETWEEN '" & dt1 & " AND " & dt2 & "'" dataAdapter.SelectCommand = command What am I missing?

            B Offline
            B Offline
            Bernhard Hiller
            wrote on last edited by
            #5

            moordoom wrote:

            connection.ConnectionString = "Data Source=SQL01_2000;database=App_DB;uid=sa;pwd=sa"

            These notes may be a little "off topic", but you should consider them: - Are you still using SQL Server 2000? (maybe I was mislead by the server name...). - using the password "sa" for the user "sa" is easy to remember, but a fun for an intruder. - do not access the database as sa, unless you really need to do so. Better create a user for the tasks required by your application.

            M 1 Reply Last reply
            0
            • B Bernhard Hiller

              moordoom wrote:

              connection.ConnectionString = "Data Source=SQL01_2000;database=App_DB;uid=sa;pwd=sa"

              These notes may be a little "off topic", but you should consider them: - Are you still using SQL Server 2000? (maybe I was mislead by the server name...). - using the password "sa" for the user "sa" is easy to remember, but a fun for an intruder. - do not access the database as sa, unless you really need to do so. Better create a user for the tasks required by your application.

              M Offline
              M Offline
              Marco Bertschi
              wrote on last edited by
              #6

              Bernhard Hiller wrote:

              - using the password "sa" for the user "sa" is easy to remember, but a fun for an intruder.

              I hope he replaced the password before he posted the code here. At least this is what I'd suggest to everyone, because anything else would be plain stupid :~:~

              The busier I am, the more I procrastinate. - Chris "The Mighty" Maunder

              M 1 Reply Last reply
              0
              • M Marco Bertschi

                Bernhard Hiller wrote:

                - using the password "sa" for the user "sa" is easy to remember, but a fun for an intruder.

                I hope he replaced the password before he posted the code here. At least this is what I'd suggest to everyone, because anything else would be plain stupid :~:~

                The busier I am, the more I procrastinate. - Chris "The Mighty" Maunder

                M Offline
                M Offline
                moordoom
                wrote on last edited by
                #7

                Yes, sa is not my user name and password. I just change all the info for the public post. And unfortunately yes, we are still using SQL 2000 SP 4. Because a previous implementer changed several stored procedures. But I will hopefully be changing that soon.

                M 1 Reply Last reply
                0
                • M moordoom

                  Yes, sa is not my user name and password. I just change all the info for the public post. And unfortunately yes, we are still using SQL 2000 SP 4. Because a previous implementer changed several stored procedures. But I will hopefully be changing that soon.

                  M Offline
                  M Offline
                  Marco Bertschi
                  wrote on last edited by
                  #8

                  moordoom wrote:

                  Yes, sa is not my user name and password. I just change all the info for the public post.

                  :thumbsup:

                  moordoom wrote:

                  And unfortunately yes, we are still using SQL 2000 SP 4. Because a previous implementer changed several stored procedures. But I will hopefully be changing that soon.

                  I'd recommend it - Because otherwise things tend to go wild if you are going to be forced into a change with a schedule which is set by Microsoft.

                  Veni, vidi, caecus

                  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