Home All Groups Group Topic Archive Search About

non-labor intensive solution to Nulls in legacy DB

Author
3 Jan 2005 7:43 PM
T Ralya

I don't want to enter the religious wars about NULLS and whether they should
or should not be allowed in Databases.  I have a legacy database and I can't
change it.  NULLS are a fact of life for me.

My question is whether anyone has a less labor intensive solution. 

In the auto-generated class definition code for the datarow of eacch table
is code like the following...

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

I have been modifying this code to...
Public Property Birthday As Date
            Get
                Try
                    Return CType(Me(Me.tableo_Person.BirthdayColumn),Date)
                Catch e As InvalidCastException
                    return nothing
                End Try
            End Get
            Set
                if value = nothing then
                   Me(Me.tableo_Person.BirthdayColumn) = system.convert.dbnull
                else
                   Me(Me.tableo_Person.BirthdayColumn) = value
                endif
            End Set
        End Property

This code gets generated anytime I have to redrag a datatable into a
dataset.  Then I have to go thru each field and modify as above again.

Is there a smarter way to do this?  (I'm normally a foxpro programmer, but
this one is going against a SQL server database, if that has any bearing on
this question)
--
T Ralya, new .NET developer, HMSA
Author
3 Jan 2005 8:07 PM
W.G. Ryan eMVP
Since you're using VB.nET you can easily use IIF with IsDBNull
http://www.knowdotnet.com/articles/handlingnullvalues.html

Show quoteHide quote
"T Ralya" <TRa***@discussions.microsoft.com> wrote in message
news:A1A63330-E4F1-4827-AF79-FE44E8A1238C@microsoft.com...
> I don't want to enter the religious wars about NULLS and whether they
should
> or should not be allowed in Databases.  I have a legacy database and I
can't
> change it.  NULLS are a fact of life for me.
>
> My question is whether anyone has a less labor intensive solution.
>
> In the auto-generated class definition code for the datarow of eacch table
> is code like the following...
>
>  Public Property Birthday As Date
>             Get
>                 Try
>                     Return CType(Me(Me.tableo_Person.BirthdayColumn),Date)
>                 Catch e As InvalidCastException
>                     Throw New StrongTypingException("Cannot get value
> because it is DBNull.", e)
>                 End Try
>             End Get
>             Set
>                 Me(Me.tableo_Person.BirthdayColumn) = value
>             End Set
>         End Property
>
> I have been modifying this code to...
>  Public Property Birthday As Date
>             Get
>                 Try
>                     Return CType(Me(Me.tableo_Person.BirthdayColumn),Date)
>                 Catch e As InvalidCastException
>                     return nothing
>                 End Try
>             End Get
>             Set
>                 if value = nothing then
>                    Me(Me.tableo_Person.BirthdayColumn) =
system.convert.dbnull
Show quoteHide quote
>                 else
>                    Me(Me.tableo_Person.BirthdayColumn) = value
>                 endif
>             End Set
>         End Property
>
> This code gets generated anytime I have to redrag a datatable into a
> dataset.  Then I have to go thru each field and modify as above again.
>
> Is there a smarter way to do this?  (I'm normally a foxpro programmer, but
> this one is going against a SQL server database, if that has any bearing
on
> this question)
> --
> T Ralya, new .NET developer, HMSA
Are all your drivers up to date? click for free checkup

Author
3 Jan 2005 8:35 PM
John Saunders
Show quote Hide quote
"T Ralya" <TRa***@discussions.microsoft.com> wrote in message
news:A1A63330-E4F1-4827-AF79-FE44E8A1238C@microsoft.com...
>I don't want to enter the religious wars about NULLS and whether they
>should
> or should not be allowed in Databases.  I have a legacy database and I
> can't
> change it.  NULLS are a fact of life for me.
>
> My question is whether anyone has a less labor intensive solution.
>
> In the auto-generated class definition code for the datarow of eacch table
> is code like the following...
>
> Public Property Birthday As Date
>            Get
>                Try
>                    Return CType(Me(Me.tableo_Person.BirthdayColumn),Date)
>                Catch e As InvalidCastException
>                    Throw New StrongTypingException("Cannot get value
> because it is DBNull.", e)
>                End Try
>            End Get
>            Set
>                Me(Me.tableo_Person.BirthdayColumn) = value
>            End Set
>        End Property

When I generate a strongly-typed DataSet from a database which has columns
which allow NULL, it also generates an Is<columnName>Null method. Do you
have such a method? If not, I wonder why it didn't think the Birthday column
allowed NULL?

John Saunders
Author
3 Jan 2005 9:01 PM
T Ralya
I hadn't noticed that procedure before, but yes it did generate it.  I can
use that to handle the situation ourside of the dataset.vb code.  Thanks
bunches.

Show quoteHide quote
"John Saunders" wrote:

> "T Ralya" <TRa***@discussions.microsoft.com> wrote in message
> news:A1A63330-E4F1-4827-AF79-FE44E8A1238C@microsoft.com...
> >I don't want to enter the religious wars about NULLS and whether they
> >should
> > or should not be allowed in Databases.  I have a legacy database and I
> > can't
> > change it.  NULLS are a fact of life for me.
> >
> > My question is whether anyone has a less labor intensive solution.
> >
> > In the auto-generated class definition code for the datarow of eacch table
> > is code like the following...
> >
> > Public Property Birthday As Date
> >            Get
> >                Try
> >                    Return CType(Me(Me.tableo_Person.BirthdayColumn),Date)
> >                Catch e As InvalidCastException
> >                    Throw New StrongTypingException("Cannot get value
> > because it is DBNull.", e)
> >                End Try
> >            End Get
> >            Set
> >                Me(Me.tableo_Person.BirthdayColumn) = value
> >            End Set
> >        End Property
>
> When I generate a strongly-typed DataSet from a database which has columns
> which allow NULL, it also generates an Is<columnName>Null method. Do you
> have such a method? If not, I wonder why it didn't think the Birthday column
> allowed NULL?
>
> John Saunders
>
>
>

Bookmark and Share