Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Min() / Max() date cast error from type DBNull

Min() / Max() date cast error from type DBNull

Scheduled Pinned Locked Moved ASP.NET
databasesql-serversysadminhelp
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bubberz
    wrote on last edited by
    #1

    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

    C 1 Reply Last reply
    0
    • B bubberz

      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

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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++

      B 1 Reply Last reply
      0
      • C Christian Graus

        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++

        B Offline
        B Offline
        bubberz
        wrote on last edited by
        #3

        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.

        C 1 Reply Last reply
        0
        • B bubberz

          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.

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          Is WBSNumber a string in the database ? If not, remove the quotes from the query. Christian Graus - Microsoft MVP - C++

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups