sanjshah12
asked on
Loop Date records as Dates
Hi,
I am trying to retrieve loop a series of records as dates, the field eventDate is set to smalldatetime but I receive an error:
Any help is appreciated.
I am trying to retrieve loop a series of records as dates, the field eventDate is set to smalldatetime but I receive an error:
ex = {"Conversion from type 'Date' to type 'Long' is not valid."}
Public Function GetDisabledDates() As IEnumerable(Of DateTime)
Dim disabledDates As New List(Of DateTime)()
'This works if added statically
'disabledDates.Add(New DateTime(DateTime.Today.Year, DateTime.Today.Month, 7))
Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("ProjectConnectionString").ConnectionString)
conn.Open()
Dim cmd As SqlCommand = conn.CreateCommand()
cmd.CommandText = "SELECT * FROM [tbl_DisabledDates] WHERE [eventDate] >= @Date"
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@Date", Today)
Dim reader As SqlDataReader = cmd.ExecuteReader()
Try
While reader.Read()
disabledDates.Add(New DateTime(reader("EventDate")))
End While
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
reader.Close()
conn.Close()
Return disabledDates
End Function
Any help is appreciated.
ASKER
Thanks Jim for your reply.
I will amend the query as per your suggestion.
This is VB.Net
I will amend the query as per your suggestion.
This is VB.Net
< Added Access zone, deleted a couple of others >
Removed access zone, added vb.net
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Gustav, that worked.
You are welcome!
For starters, I'm guessing you want to have the @Date outside of the double-quotes, and convert it to a string so you can concatenate it with the rest of the T-SQL, like this..
cmd.CommandText = "SELECT * FROM [tbl_DisabledDates] WHERE [eventDate] >= '" + CStr(@Date as string) + "'"
btw Is this Access VBA or VB.NET? Neither zone is referenced in this question.