Min() / Max() date cast error from type DBNull
-
When I run the following code I get: Cast from type 'DBNull' to type 'Date' is not valid. The datatype for the field is smalldatetime. I can run it fine from SQL Server Dim strSQL As String strSQL = "SELECT MIN(ESDate) AS MinDate, MAX(EFDate) AS MaxDate FROM P3ActWBS WHERE " strSQL &= "WBSNumber = '" & strWBSN & "'" Dim cmd2 As New SqlCommand(strSQL, sCon1) Dim DR2 As SqlDataReader = cmd2.ExecuteReader() DR2.Read() If DR2.HasRows = True Then Dim strMinDate As Date = DR2(0) Dim strMaxDate As Date = DR2(1) DR2.Close() DR2 = Nothing
-
When I run the following code I get: Cast from type 'DBNull' to type 'Date' is not valid. The datatype for the field is smalldatetime. I can run it fine from SQL Server Dim strSQL As String strSQL = "SELECT MIN(ESDate) AS MinDate, MAX(EFDate) AS MaxDate FROM P3ActWBS WHERE " strSQL &= "WBSNumber = '" & strWBSN & "'" Dim cmd2 As New SqlCommand(strSQL, sCon1) Dim DR2 As SqlDataReader = cmd2.ExecuteReader() DR2.Read() If DR2.HasRows = True Then Dim strMinDate As Date = DR2(0) Dim strMaxDate As Date = DR2(1) DR2.Close() DR2 = Nothing
bubberz wrote: When I run the following code I get: Cast from type 'DBNull' to type 'Date' is not valid. Are you sure the query you run in SQL Server is the same ? Can these columns contain nulls ? Christian Graus - Microsoft MVP - C++
-
bubberz wrote: When I run the following code I get: Cast from type 'DBNull' to type 'Date' is not valid. Are you sure the query you run in SQL Server is the same ? Can these columns contain nulls ? Christian Graus - Microsoft MVP - C++
-
That's what's weird.....I get two columns back with the correct date values when run from Enterprise Manager, and I know the where clause is correct.
Is WBSNumber a string in the database ? If not, remove the quotes from the query. Christian Graus - Microsoft MVP - C++