title: Conversion from type 'DBNull' to type 'Date' is not valid
body: <p>I am getting this exception from the following VB.NET code for only certain months:</p>
  
  <pre><code>System.InvalidCastException: Conversion from type 'DBNull' to type 'Date' is not valid.
  </code></pre>
  
  <p>It happens on the line: </p>
  
  <pre><code>If CDate(dRow("CompleteDate")).ToString("d") = arrWeekYear(i, 1).ToString("d") Then
  </code></pre>
  
  <p>If I understand this correctly, then the problem is I am trying to compare some NULL values for CompleteDate to a non-NULL value.</p>
  
  <p>How do I fix this?  For some dates, this code above runs perfectly.  However for some of the more recent records, it does not.  But I am able to get output from the T-SQL query with same date range and there are no errors; it runs quickly also.  And I examined this T-SQL query for both "Date_Completed" and "Review_Date" NULL values, but either way, "CompleteDate" was always = NON-Null value.  So I do not understand how this is happening.</p>
  
  <p>Here is my VB.NET query:</p>
  
  <pre><code>            commandstring = "SELECT Batch_Records.Part_Number, Batch_Records.Lot_Number, Batch_Records.Date_Received, " &amp; _
                  "IsNull([Date_Completed], [Review_Date]) AS [CompleteDate], Batch_Records.Error, " &amp; _
                  "Batch_Records.[Group], Batch_Records.MFG, Batch_Records.MFG2, Batch_Records.QC, Batch_Records.QC2, " &amp; _
                  "QC_CODES.CODE_DESC, DATEADD(DD, 1 - DATEPART(DW, Batch_Records.Date_Received), Batch_Records.Date_Received) AS SundayDate " &amp; _
                  "FROM EXCEL.Batch_Records LEFT JOIN EXCEL.QC_CODES ON Batch_Records.Part_Number = QC_CODES.CODE_ID " &amp; _
                  "WHERE (Batch_Records.[Group]" &amp; TheGroup &amp; " AND Batch_Records.Date_Received &gt; '" &amp; FirstWeek &amp; "' AND Batch_Records.Date_Received &lt; dateadd(Day, 1, '" &amp; LastWeek &amp; "'))" &amp; _
                     "ORDER BY Batch_Records.Date_Received"
  </code></pre>
  
  <p>When I add this line above the error-causing line above, my report times out for these months.  So what to do?</p>
  
  <pre><code>If Not dRow("CompleteDate") Is System.DBNull.Value Then
  </code></pre>