S
Sören Nils Kuklau
Hi,
when pulling data from the database, we have plenty of cases where we
create a DataSet, then iterate through the rows, like:
Dim ds As DataSet = someFunction()
For Each currentRow As DataRow In ds.Tables(0).Rows
We then fill internal properties with the returned cells, like:
someObject.name = myRow("name")
The data is typically a string or an integer.
However, there are a few edge cases where the row in the database is
actually NULL. Normally this causes an exception; we work around this
by wrapping myRow() access in CheckDBNullInteger and CheckDBNullString
calls; the functions are simply:
Public Function CheckDBNullInteger(ByVal o As Object) As Integer
If o Is DBNull.Value Then
Return 0
Else
Return CInt(o)
End If
End Function
Public Function CheckDBNullString(ByVal o As Object) As String
If o Is DBNull.Value Then
Return ""
Else
Return o.ToString
End If
End Function
That works fine, but it's not pretty. I'm wondering if there is a way
to either implicitly call this function (by subclassing DataRow,
perhaps?), or solve this more elegantly in some other fashion.
Thanks!
when pulling data from the database, we have plenty of cases where we
create a DataSet, then iterate through the rows, like:
Dim ds As DataSet = someFunction()
For Each currentRow As DataRow In ds.Tables(0).Rows
We then fill internal properties with the returned cells, like:
someObject.name = myRow("name")
The data is typically a string or an integer.
However, there are a few edge cases where the row in the database is
actually NULL. Normally this causes an exception; we work around this
by wrapping myRow() access in CheckDBNullInteger and CheckDBNullString
calls; the functions are simply:
Public Function CheckDBNullInteger(ByVal o As Object) As Integer
If o Is DBNull.Value Then
Return 0
Else
Return CInt(o)
End If
End Function
Public Function CheckDBNullString(ByVal o As Object) As String
If o Is DBNull.Value Then
Return ""
Else
Return o.ToString
End If
End Function
That works fine, but it's not pretty. I'm wondering if there is a way
to either implicitly call this function (by subclassing DataRow,
perhaps?), or solve this more elegantly in some other fashion.
Thanks!