DotNetNewsgroup.com  
web access to complete list of Microsoft.NET newsgroups
   home   |   control panel login   |   archive  |  
 
  carried group
academic
adonet
aspnet
aspnet.announcements
aspnet.buildingcontrols
aspnet.caching
aspnet.datagridcontrol
aspnet.mobile
aspnet.security
aspnet.webcontrols
aspnet.webservices
assignment_manager
datatools
dotnet.distributed_apps
dotnet.general
dotnet.myservices
dotnet.nternationalization
dotnet.scripting
dotnet.security
dotnet.vjsharp
dotnet.vsa
dotnet.xml
dotnetfaqs
framework
framework.clr
framework.compactframework
framework.component_services
framework.controls
framework.databinding
framework.drawing
framework.enhancements
framework.interop
framework.odbcnet
framework.performance
framework.remoting
framework.sdk
framework.setup
framework.webservices
framework.windowsforms
framework.wmi
frwk.windowsforms.designtime
lang.csharp
lang.jscript
lang.vb
lang.vb.controls
lang.vb.data
lang.vb.upgrade
lang.vc
lang.vc.libraries
  
 
start date: Wed, 22 Aug 2007 14:28:01 -0700,    posted on: microsoft.public.dotnet.languages.vb        back       

Thread Index
  1    Terry am
          2    Armin Zingler
                 3    Terry am
                 4    (Linda Liu [MSFT])


Is this a bug or what am I missing?   
I am having problems with the following code and hope someone can explain 
this to me.  Here is the routine that is having the problems:

    Private Shared Sub LoadSubscriptionFromDataRow(ByVal s As Subscription, 
ByVal dr As DataRow)
        With dr
            With s
                .SubscriptionId = .Item(CN_SubscriptionId)
                .UserId = CType(.Item(CN_UserId), Integer)
                .BillToId = CType(.Item(CN_BillToId), Integer)
 *              .OrderDate = CType(.Item(CN_OrderDate), Date)
                .ProductId = .Item(CN_ProductId)
                .LicQty = CType(.Item(CN_LicQty), Short)
  *             .RenewalDate = CType(.Item(CN_RenewalDate), Nullable(Of Date))
                .LastInvType = .Item(CN_LastInvType)
                .LastInvSeq = CType(.Item(CN_LastInvSeq), Short)
                .AbaNo = .Item(CN_AbaNo)
                .TaxStatus = .Item(CN_TaxStatus)
                .BillingComment = .Item(CN_BillingComment)
 * *           .CancelDate = CType(.Item(CN_CancelDate), Nullable(Of Date))
                .CancelCode = .Item(CN_CancelCode)
 *              .LastMaintDate = CType(.Item(CN_LastMaintDate), Date)
            End With
        End With
    End Sub

Each of the lines flaged above throws an execption.  

Conversion from string "" to type 'Date' is not valid.
but upon examinatiopn of one of the data items, it is a valid date:
+dr.Item("OrderDate")	#5/23/1990# {Date}	Object
	Date	#5/23/1990#	Date
and the funny thing is that by changing the line to ....dr.item(... the 
exception goes away!  So, it must have someting to do with the 'with dr'.  
Also funny that the problem only occurs on 'Date' types.

On the line with 2 '**'s, the above exception changes to:

System.InvalidCastException was unhandled
  Message="Specified cast is not valid."

the value is System.VBNull.  I guess I have to handle the case where the 
date comming from the DB is null in some other way, but I thought this was 
the reason for Nullable(Of T).

-- 
Terry
Date:Wed, 22 Aug 2007 14:28:01 -0700   Author:  

Re: Is this a bug or what am I missing?   
"Terry" <TerryL@nospam.nospam> schrieb

> I am having problems with the following code and hope someone can
> explain this to me.  Here is the routine that is having the
> problems:
>
>    Private Shared Sub LoadSubscriptionFromDataRow(ByVal s As
> Subscription, ByVal dr As DataRow)
>        With dr
>            With s
>                .SubscriptionId = .Item(CN_SubscriptionId)
>                .UserId = CType(.Item(CN_UserId), Integer)
>                .BillToId = CType(.Item(CN_BillToId), Integer)
> *              .OrderDate = CType(.Item(CN_OrderDate), Date)
>                .ProductId = .Item(CN_ProductId)
>                .LicQty = CType(.Item(CN_LicQty), Short)
>  *             .RenewalDate = CType(.Item(CN_RenewalDate),
> Nullable(Of Date))                .LastInvType =
> .Item(CN_LastInvType)
>                .LastInvSeq = CType(.Item(CN_LastInvSeq), Short)
>                .AbaNo = .Item(CN_AbaNo)
>                .TaxStatus = .Item(CN_TaxStatus)
>                .BillingComment = .Item(CN_BillingComment)
> * *           .CancelDate = CType(.Item(CN_CancelDate), Nullable(Of
> Date))                .CancelCode = .Item(CN_CancelCode)
> *              .LastMaintDate = CType(.Item(CN_LastMaintDate), Date)
>            End With
>        End With
>    End Sub
>
> Each of the lines flaged above throws an execption.
>
> Conversion from string "" to type 'Date' is not valid.
> but upon examinatiopn of one of the data items, it is a valid date:
> +dr.Item("OrderDate") #5/23/1990# {Date} Object
> Date #5/23/1990# Date
> and the funny thing is that by changing the line to ....dr.item(...
> the exception goes away!


Not funny. You are nesting With blocks which doesn't makes sense in this
case.

With dr
   With s
      .OrderDate = CType(.Item(CN_OrderDate), Date)
   end with
end with

You are referring to s on the left and on the right side of the "=". The
DataRow is not used at all. So, this is the same as

s.OrderDate = CType(s.Item(CN_OrderDate), Date)

Probably the "Subscription" class has an Item property where you can pass a
Date but CN_OrderDate is a String. Therefore the error.



>  So, it must have someting to do with the
> 'with dr'. Also funny that the problem only occurs on 'Date' types.
>
> On the line with 2 '**'s, the above exception changes to:
>
> System.InvalidCastException was unhandled
>  Message="Specified cast is not valid."
>
> the value is System.VBNull.


System.DBNull.Value?


> I guess I have to handle the case where
> the date comming from the DB is null in some other way,

Correct

> but I
> thought this was the reason for Nullable(Of T).


Nullable has no relation to DBNull. In the Framework (and other languages), 
the term "Null" is used for "no reference", which is "Nothing" in VB. So, 
Nullable(Of Date) means that it can be either Nothing or a Date. It can 
never be DBNull.Value.
So,

    if DatabaseValue is dbnull.value then
        .NullableOfDateField = Nothing
    else
        .NullableOfDateField = CDate(DataBaseValue)
    end if


Armin
Date:Thu, 23 Aug 2007 00:10:19 +0200   Author:  

Re: Is this a bug or what am I missing?   
Hi Armin,
   Thanks for the quick reply.  Thats what I get for using someone elses 
code to start with.  S inherits from a class that has an Item property, and I 
missed that.  I am still confused as to why it is only with the date 
conversions that the problem shows up.
   On the second issue, thanks - got it.
-- 
Terry


"Armin Zingler" wrote:


> "Terry" <TerryL@nospam.nospam> schrieb
> > I am having problems with the following code and hope someone can
> > explain this to me.  Here is the routine that is having the
> > problems:
> >
> >    Private Shared Sub LoadSubscriptionFromDataRow(ByVal s As
> > Subscription, ByVal dr As DataRow)
> >        With dr
> >            With s
> >                .SubscriptionId = .Item(CN_SubscriptionId)
> >                .UserId = CType(.Item(CN_UserId), Integer)
> >                .BillToId = CType(.Item(CN_BillToId), Integer)
> > *              .OrderDate = CType(.Item(CN_OrderDate), Date)
> >                .ProductId = .Item(CN_ProductId)
> >                .LicQty = CType(.Item(CN_LicQty), Short)
> >  *             .RenewalDate = CType(.Item(CN_RenewalDate),
> > Nullable(Of Date))                .LastInvType =
> > .Item(CN_LastInvType)
> >                .LastInvSeq = CType(.Item(CN_LastInvSeq), Short)
> >                .AbaNo = .Item(CN_AbaNo)
> >                .TaxStatus = .Item(CN_TaxStatus)
> >                .BillingComment = .Item(CN_BillingComment)
> > * *           .CancelDate = CType(.Item(CN_CancelDate), Nullable(Of
> > Date))                .CancelCode = .Item(CN_CancelCode)
> > *              .LastMaintDate = CType(.Item(CN_LastMaintDate), Date)
> >            End With
> >        End With
> >    End Sub
> >
> > Each of the lines flaged above throws an execption.
> >
> > Conversion from string "" to type 'Date' is not valid.
> > but upon examinatiopn of one of the data items, it is a valid date:
> > +dr.Item("OrderDate") #5/23/1990# {Date} Object
> > Date #5/23/1990# Date
> > and the funny thing is that by changing the line to ....dr.item(...
> > the exception goes away!
> 
> Not funny. You are nesting With blocks which doesn't makes sense in this
> case.
> 
> With dr
>    With s
>       .OrderDate = CType(.Item(CN_OrderDate), Date)
>    end with
> end with
> 
> You are referring to s on the left and on the right side of the "=". The
> DataRow is not used at all. So, this is the same as
> 
> s.OrderDate = CType(s.Item(CN_OrderDate), Date)
> 
> Probably the "Subscription" class has an Item property where you can pass a
> Date but CN_OrderDate is a String. Therefore the error.
> 
> 
> >  So, it must have someting to do with the
> > 'with dr'. Also funny that the problem only occurs on 'Date' types.
> >
> > On the line with 2 '**'s, the above exception changes to:
> >
> > System.InvalidCastException was unhandled
> >  Message="Specified cast is not valid."
> >
> > the value is System.VBNull.
> 
> System.DBNull.Value?
> 
> > I guess I have to handle the case where
> > the date comming from the DB is null in some other way,
> 
> Correct
> 
> > but I
> > thought this was the reason for Nullable(Of T).
> 
> Nullable has no relation to DBNull. In the Framework (and other languages), 
> the term "Null" is used for "no reference", which is "Nothing" in VB. So, 
> Nullable(Of Date) means that it can be either Nothing or a Date. It can 
> never be DBNull.Value.
> So,
> 
>     if DatabaseValue is dbnull.value then
>         .NullableOfDateField = Nothing
>     else
>         .NullableOfDateField = CDate(DataBaseValue)
>     end if
> 
> 
> Armin
> 
> 
Date:Wed, 22 Aug 2007 15:34:00 -0700   Author:  

Re: Is this a bug or what am I missing?   
Hi Terry,


>  I am still confused as to why it is only with the date conversions that 

the problem shows up.

Perhaps other properties, such as 'Subscription', 'UserId', "BillTold" and 
ect in the Subscription instance have correct types and initial value. 

Hope this helps.

If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to 
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
 
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues 
where an initial response from the community or a Microsoft Support 
Engineer within 1 business day is acceptable. Please note that each follow 
up response may take approximately 2 business days as the support 
professional working with you may need further investigation to reach the 
most efficient resolution. The offering is not appropriate for situations 
that require urgent, real-time or phone-based interactions or complex 
project analysis and dump analysis issues. Issues of this nature are best 
handled working with a dedicated Microsoft Support Engineer by contacting 
Microsoft Customer Support Services (CSS) at 
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
 
This posting is provided "AS IS" with no warranties, and confers no rights.
Date:Thu, 23 Aug 2007 08:54:41 GMT   Author:  

Google
 
Web dotnetnewsgroup.com


COPYRIGHT ?2005, EUROFRONT WORLDWIDE LTD., ALL RIGHT RESERVE  |   Contact us