HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Set default value for NULL in datatable

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
datatablenullvaluedefaultforset

Problem

I have the following codes, which I created as extension for datatable in my project.
It actually worked. Just wonder if there is any optimization can be made through. Thanks. =)


Public Function HasNull(ByVal dataTable As DataTable) As Boolean
    For Each column As DataColumn In dataTable.Columns
        If dataTable.Rows.OfType(Of DataRow)().Any(Function(r) r.IsNull(column)) Then
            Return True
        End If
    Next
    Return False
End Function

Public Function SetDefaultForNull(ByVal dataTable As DataTable) As DataTable
    For Each row As DataRow In dataTable.Rows
        For Each col As DataColumn In dataTable.Columns
            Dim value As Object = row(col)
            If IsDBNull(value) Then
                Dim dataType As String = col.DataType.ToString

                Select Case dataType
                    Case "System.DateTime"
                        value = New DateTime
                    Case "System.Decimal", "System.Int16", "System.Int32", "System.Int64"
                        value = 0
                    Case "System.String"
                        value = String.Empty
                    Case "System.Boolean"
                        value = False
                    Case Else
                        value = 0
                End Select
                row(col) = value
            End If
        Next
    Next
    Return dataTable
End Function

Solution

A couple of small points.

  • Single letter identifiers and variable names are the devil incarnate. Replace r with something meaningful.



-
There's not much point in using a variable for col.DataType.ToString. You only use the variable dataType once, so it would be perfectly acceptable to get rid of a line of a code and do this instead.

Select Case col.DataType.ToString
    Case "System.DateTime"
        value = New DateTime


-
I also don't see much benefit to the value variable. You could just set row(col) = to whatever you would set value to.

-
There's no reason to explicitly check for Case "System.Decimal", "System.Int16", "System.Int32", "System.Int64". It's taken care of in the Else case.

Select Case col.DataType.ToString
    Case "System.DateTime"
        row(col) = New DateTime
    Case "System.String"
        row(col) = String.Empty
    Case "System.Boolean"
        row(col) = False
    Case Else
        row(col) = 0
End Select

Code Snippets

Select Case col.DataType.ToString
    Case "System.DateTime"
        value = New DateTime
Select Case col.DataType.ToString
    Case "System.DateTime"
        row(col) = New DateTime
    Case "System.String"
        row(col) = String.Empty
    Case "System.Boolean"
        row(col) = False
    Case Else
        row(col) = 0
End Select

Context

StackExchange Code Review Q#58024, answer score: 4

Revisions (0)

No revisions yet.