Solved

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

Posted on 2004-10-01
3
1,019 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
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 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