Reputation: 39413
I've got a new instance of an object in a Linq DBML, but don't know of the test if the a date field is empty.
I have tried:
If d Is Nothing OrElse IsDBNull(d) OrElse d <= Date.MinValue Then
TextValue = "nada"
Else
TextValue = FormatDateTime(d)
End If
But the result is "12:00:00 am"
edit: Changed variable name the d
edit: Please note that the SQL field is nullable
edit II: Sorry guys, the error was in other part of the code. Comparing to nothing is the solution
Upvotes: 1
Views: 2865
Reputation: 74530
I'm not sure if this will work, but you probably want to compare against Empty (or make a call to IsEmpty) as 12:00 am is NOT a null value.
Upvotes: 0
Reputation: 6659
The code you want is (hopefully)..
If MyNullableDateObject.HasValue Then
TextValue = FormatDateTime(MyNullableDateObject.Value)
Else
TextValue = "nada"
End If
If the SQL field accepts Nulls then the Linq to SQL designer maps it to a .net Nullable type, for instance SQL DateTime will be mapped to the type called Date?. If you look at the properties of the field in the Linq to SQL designer it should have a property "Nullable" which, if the SQL schema allows NULLs in that field, will be set to true.
Non-Nullable (i.e. ordinary) date types are Value types not Object types so "Is Nothing" doesn't help. IsDBNull is for types under System.Data.SqlTypes, but Linq to SQL maps to "native" .Net types so that doesn't help either.
See - Nulllable Types
EDIT
You might consider renaming the variable Value to something else, (as I have done above) since of course, it isn't a Value type, Value is bad name.
Also Note that in VB Nullable type can be declared in two equivalent ways
Dim d As Date?
Dim d As Nullable(Of Date)
Hope this makes sense.
Upvotes: 3