DateTimePicker syntax error

Moordoom

Member
Joined
Nov 12, 2013
Messages
23
Programming Experience
1-3
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
VB.NET:
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
VB.NET:
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...

VB.NET:
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?
I put this in the Forms Forum, but it may belong in the SQL one.
 
Last edited:
Got it figured out. Always forget about parameters...

VB.NET:
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
 
Back
Top