List Info

Thread: Assigning a value to a nullable type from a datareader




Assigning a value to a nullable type from a datareader
user name
2006-07-24 16:45:05
I've got a field defined as follows:

    Private MyApprovedById As Nullable(Of Integer)

which I access using the following property:

    Public Property ApprovedById() As Nullable(Of Integer)
        Get
            Return MyApprovedById
        End Get
        Private Set(ByVal value As Nullable(Of Integer))
            If value.HasValue AndAlso value.Value < 0
Then
                Throw New
ArgumentException("ApprovedById < 0.",
"value")
            End If
            MyApprovedById = value
        End Set
    End Property

In a Shared method where I create an instance of the class,
I have the
following code:

   NewSecuritization.ApprovedById =
CType(.Item("ApprovedByID"),
Nullable(Of Integer)).GetValueOrDefault(Nothing)

which is failing with an invalid cast error.

The debugger says
DataReader.Item("ApprovedByID") has a value of
System.DBNull.

I get the same error if I specify a default value of -1.

What am I doing wrong?

Thanks,

Bob


************************************************************
*****************
If you are not the intended recipient of this e-mail, please
notify 
the sender immediately. The contents of this e-mail do not
amend 
any existing disclosures or agreements unless expressly
stated.
************************************************************
*****************

===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Assigning a value to a nullable type from a datareader
user name
2006-07-24 18:29:56
Ah, I figured it out now. You can't get there from here :-(

I still have to check for DBNull and either assign the value
or Nothing.
I was hoping for better. But at least now I don't need a
flag variable
that indicates the column was null.

Bob
 

-----Original Message-----
From: Discussion of writing applications and components
using Visual
Basic .NET [mailto:VBDOTNETDISCUSS.DEVELOP.COM] On
Behalf Of Ainsley,
Robert, ISD
Sent: Monday, July 24, 2006 11:45 AM
To: VBDOTNETDISCUSS.DEVELOP.COM
Subject: [VBDOTNET] Assigning a value to a nullable type
from a
datareader

I've got a field defined as follows:

    Private MyApprovedById As Nullable(Of Integer)

which I access using the following property:

    Public Property ApprovedById() As Nullable(Of Integer)
        Get
            Return MyApprovedById
        End Get
        Private Set(ByVal value As Nullable(Of Integer))
            If value.HasValue AndAlso value.Value < 0
Then
                Throw New
ArgumentException("ApprovedById < 0.",
"value")
            End If
            MyApprovedById = value
        End Set
    End Property

In a Shared method where I create an instance of the class,
I have the
following code:

   NewSecuritization.ApprovedById =
CType(.Item("ApprovedByID"),
Nullable(Of Integer)).GetValueOrDefault(Nothing)

which is failing with an invalid cast error.

The debugger says
DataReader.Item("ApprovedByID") has a value of
System.DBNull.

I get the same error if I specify a default value of -1.

What am I doing wrong?

Thanks,

Bob


************************************************************
************
*****
If you are not the intended recipient of this e-mail, please
notify the
sender immediately. The contents of this e-mail do not amend
any
existing disclosures or agreements unless expressly stated.
************************************************************
************
*****

===================================
This list is hosted by DevelopMentor(r)  http://www.develop.com

View archives and manage your subscription(s) at
http://discuss.develop.com


===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )