I have a basic vb.net program that pulls a query from an SQL database. My program works correctly if I hard code the date, however when I change the code from:
Dim dtstartdate As String = DateTime.Today
Dim dttomorrow As DateTime = DateTime.Today.AddDays(1)
Dim dtenddate As DateTime = dttomorrow.AddSeconds(-1)
Try
For icounter = 1 To 2
Call GetLocationInfo()
connectionString = "Data Source=" & LocationDB & ";Initial Catalog=database;Persist Security Info=True;User ID=login;Password=password"
sql = "select count(sTicket_number) as tickets from tickets where dtcreated between 2/8/2014 AND 2/9/2014 "
sqlCnn = New SqlConnection(connectionString)
sqlCnn.Open()
TO:
Dim dtstartdate As String = DateTime.Today
Dim dttomorrow As DateTime = DateTime.Today.AddDays(1)
Dim dtenddate As DateTime = dttomorrow.AddSeconds(-1)
Try
For icounter = 1 To 2
Call GetLocationInfo()
connectionString = "Data Source=" & LocationDB & ";Initial Catalog=database;Persist Security Info=True;User ID=login;Password=password"
sql = "select count(sTicket_number) as tickets from tickets where dtcreated between " & dtstartdate & " AND " & dtenddate & ""
sqlCnn = New SqlConnection(connectionString)
sqlCnn.Open()
I get a "Syntax Error near '11'" What am I doing incorrectly with the dtstartdate and dtenddate?
select count(sTicket_number) as tickets from tickets where dtcreated between '" & dtstartdate & "' AND '" & dtenddate & "'"?