Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Detecting null value on Integer
Message
De
26/01/2016 16:32:55
 
 
À
26/01/2016 13:12:10
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
VB 9.0
OS:
Windows 8.1
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01630133
Message ID:
01630263
Vues:
50
>>But DataRow Items are Objects that must be cast to the relevant .NET type - and this can only be done if they are not DBNull.Value. So e.g:
dr("SomeString") = DBNull.Value
>>        Dim s As String
>>        If dr("SomeString") Is DBNull.Value Then
>>            s = Nothing
>>        Else
>>            s = CStr(dr("SomeString"))
>>        End If
(so s cannot be DBNull.Value)
>>If you're dealing with an integer data item in a DataRow you can test using:
dr.IsNull("SomeInt")
>
>I see in my code I am using it against a data row:
>
>
>                    ' Based on the type
>                    Select Case lcType
>
>                        ' Character, memo field, NVarChar, VarChar, VarChar(MAX), NVarChar(MAX)
>                        Case "C", "M", "O", "V", "X", "Y"
>
>                            ' If we have a null
>                            If oRow(lcField) Is DBNull.Value Then
>                                oRow(lcField) = ""
>                            End If
>
>
>I see the mentioned code for the string is not called by anything. So, that might explain the confusion.
>
>I do however have something like this:
>
>
>    ' Return True if a date is empty
>    ' expD1 Date
>    Public Function EmptyDate(ByVal tdDate As Object) As Boolean
>        Dim llEmpty As Boolean = False
>        Dim ldDate As Date
>
>        ' Based on the backend
>        Select Case nBackend
>
>            ' Visual FoxPro
>            Case 1
>                ldDate = New Date(1899, 12, 30)
>
>                ' If this is an empty date
>                If tdDate = ldDate Then
>                    llEmpty = True
>                End If
>
>                ' SQL Server
>            Case 2
>
>                ' If the date is null
>                If tdDate Is DBNull.Value Then
>                    llEmpty = True
>                End If
>
>                ldDate = New Date(1899, 12, 30)
>
>                ' If this is an empty date
>                If tdDate = ldDate Then
>                    llEmpty = True
>                End If
>
>                ' ODBC
>            Case 3
>
>        End Select
>
>        Return llEmpty
>    End Function
>
>
>Would you say the string explanation on null would also apply on a date? If yes, that would that make the above code obsolete?

When dealing with DataRows its much easier to use the Field (https://msdn.microsoft.com/en-us/library/bb360891%28v=vs.110%29.aspx) and SetField (https://msdn.microsoft.com/en-us/library/bb353039%28v=vs.110%29.aspx) extension methods instead of accessing the fields directly. You can use these methods to get or set the field to a string, nullable int, nullable datetime, etc. without needing to deal with DBNull at all. Sample VB code for retrieving a nullable datetime can be found at http://stackoverflow.com/questions/4158568/populating-a-nullable-type-from-a-datarow.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform