Sébastien Sébastien - 6 months ago 79
Vb.net Question

Datatable Select with Datetime

In the second

For Each
, I want to select all records with
EmailCreateDate = dtEmailCreateDate
, but no record is found. Does someone have an idea ?

Dim dtEmailCreateDate As DateTime = Date.Now
For Each row In _tblMandantenData.Select("Selected = True AND EmailSentDate IS NULL")
row("EmailCreateDate") = dtEmailCreateDate
DBAccess.UpdateEmailVersandInfos(row)
Next

Dim strQuery = String.Format("Selected = True AND EmailCreateDate = '{0}'", dtEmailCreateDate.ToString())
For Each rowToSent In _tblMandantenData.Select(strQuery)
SentMail(rowToSent)
Next

Answer

If you look at the Expression property of the DataColumn (the rules outlined there are the rules to use for the Select filter expression) you will notice that a filter on DateTime column is created enclosing the date between the number symbol (#)

 DateTimeColumn = #value#

Moreover the date should be formatted according to the format MM-dd-yyyy and thus your code should check the dates with this expression

Dim strQuery = String.Format("Selected = True AND " & _ 
               "EmailCreateDate = #{0}#", dtEmailCreateDate.ToString("MM-dd-yyyy"))    

Of course this could still fail if the column named EmailCreateDate contains also information about times and thus you should change your formatting to include also output for time "HH:mm:ss" or better use a BETWEEN syntax