Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Cast from type 'DBNull' to type 'Date' is not valid

Posted on 2004-10-01
3
Medium Priority
?
1,027 Views
Last Modified: 2008-01-09
I am new to .NET and am working on my first application.

Am using a dataset. A date field in the underlying SQL table allows nulls.

I have a web form that uses this field.

When the form encounters a record with a null value in this field I get the following message:

     System.Data.StrongTypingException: Cannot get value because it is DBNull. --->
     System.InvalidCastException: Cast from type 'DBNull' to type 'Date' is not valid. at...

It references the following code:

        Public Property dtDateModified As Date
            Get
                Try
                    Return CType(Me(Me.tableSBS_Property_Group.dtDateModifiedColumn),Date)
                Catch e As InvalidCastException
                    Throw New StrongTypingException("Cannot get value because it is DBNull.", e)
                End Try
            End Get
            Set
                Me(Me.tableSBS_Property_Group.dtDateModifiedColumn) = value
            End Set
        End Property

I have read several posts here and elsewhere for handling nulls. Unfortunately, I haven't been smart enough to successfully implement any of them.

Your help is appreciated.

John
0
Comment
Question by:JohnDesautels
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 6

Author Comment

by:JohnDesautels
ID: 12205818
I kept on searching a found the anser at http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21123520.html.

This is the code I use in my form now:
(dtDateModified is the field I am checking for nulls)

      If DataSetRow.IsdtDateModifiedNull Then
        lblModifiedData.Text = .sModifiedBy.ToString
      Else
        lblModifiedData.Text = .sModifiedBy.ToString & " - " & .dtDateModified.ToString
      End If

John
0
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
ID: 12227435
Question answered by asker or dialog valuable.
Closed, 500 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Admin
0

Featured Post

Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question