Is this a bug or what am I missing?

G

Guest

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).
 
A

Armin Zingler

Terry said:
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
 
G

Guest

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.
 
L

Linda Liu [MSFT]

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.
 
L

Linda Liu [MSFT]

Hi Terry,

How about the problem now?

If you have any question about this particular issue, please feel free to
let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top