I have the following VB.NET code where I am attempting to connect to a SQL Server table, query it to find out if a date in the table is between a derived date called "fileCreateDate" and now/today.
Sub getDateModified() Dim fileCreatedDate As DateTime = File.GetCreationTime(strFilePath + "\CPI1.csv") Dim stmt As String Dim dtRd As String SQLConn.ConnectionString = ConnString SQLConn.Open() stmt = "select Release_Date from dbo.tblRelease_Dates where Release_Date Between '" + fileCreatedDate + "' And 'CAST(GetDate() AS DATE)'" SQLCmd.Connection = SQLConn SQLCmd.CommandText = stmt SQLdr2 = SQLCmd.ExecuteReader If SQLdr2.HasRows Then While SQLdr2.Read() dtRd = SQLdr2.GetString(0) End While End If End Sub
However, the code stops with an error at SQLdr2 = SQLCmd.ExecuteReader. I'm sure the error is because of 'CAST(GetDate() AS DATE)' in the string stmt but I am unsure as to how to fix this so that the query is between fileCreateDate and today. The value being returned from fileCreateDate is 9/4/2012 10:58:03 AM and there is a date between that and now that I need. Can someone see what I am doing wrong?