Buongiorno a tutta la Community :-)
Da quando ho iniziato ad utilizzare .NET uno dei dubbi che non riesco a chiarire (ci sono molte soluzioni spesso discordanti in giro e di qualche anno fà
riguarda il metodo per stabilire se un campo di una tabella è 'DBNull' e la stessa cosa con le DGV (associate/non associate ad un DataTable).
Secondo voi quale tra i seguenti è il metodo più 'corretto' e che non sollevi eccezioni ?
esempio :
Dim dt As DataTable = New DataTable()
Using connDB As OleDbConnection = New OleDbConnection(strConn)
Try
connDB.Open()
Dim strSQL As String = "Select * from ANAG WHERE cod_int = "& intCand
Dim aCommandin As OleDbCommand = New OleDbCommand(strSQL, connDB)
Using aReader As OleDbDataReader = aCommandin.ExecuteReader()
dt.Load(aReader)
End Using
If (dt.Rows.Count > 0) Then
If dt.Rows(0).Item("datiscr").ToString <>"" Then datiscr.MyDate.Text = Format(dt.Rows(0).Item("datiscr"), "dd/MM/yyyy") ' campo tipo DATA
If IsDBNull(dt.Rows(0).Item("note")) = False Then txtNote.Text = dt.Rows(0).Item("note").ToString ' campo tipo TEXT
If IsDBNull(dt.Rows(0).Item("n_reg")) = False Then ' campo tipo NUMERICO
If dt.Rows(0).Item("n_reg") <> 0 Then nreg.Text = dt.Rows(0).Item("n_reg").ToString
End If
' OPPURE
If dt.Rows(0).Item("datiscr") IsNot Nothing Then datiscr.MyDate.Text = Format(dt.Rows(0).Item("datiscr"), "dd/MM/yyyy") ' campo tipo DATA
'OPPURE
If Not DBNull.Value.Equals(dt.Rows(0).Item("datiscr")) Then ' campo DATA
'OPPURE
' ho trovato questa funzioncina che sembra funzionare per controllare se in campo è DBNull o Stringa Vuota
If dt.Rows(0).Item("note").ToString.IsDBNullOrEmpty THEN ....
Public Function IsDBNullOrEmpty(ByVal obj As [Object]) As Boolean
Return (obj.Equals(DBNull.Value) Or (Not obj.Equals(DBNull.Value) AndAlso String.IsNullOrEmpty(obj)))
End Function
I dubbi persistono anche con le DGV. In alcuni casi funzionano tutte in altre viene sollevata un eccezione riguardo proprio i DBNull.
Attendo i vostri prezioni pareri.