• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 606
  • Last Modified:

Dealing with Nulls and Decimals.

We had a contractor that did some work for us and I see a block of code I have never worked with. I am getting an error on the Safety Variable that doesn't like nulls. I don't quite understand how to deal with it. It's in the footer code. This line

Dim safety As Decimal = Math.Round(CDec(data.Tables(1).Compute("SUM(Safety) / SUM(Shifts)", "Unit <> 'Total'")), 2)
 The error I get is
Conversion from type 'DBNull' to type 'Decimal' is not valid.
Now I believe I am dealing with the nulls after I collect the data, but I must not be.

 Protected Sub grdReport_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdReport.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then

            Dim row As DataRowView = e.Row.DataItem

            With e.Row

                If row("Name") = "TOTAL" Then .Cells(0).Style.Add("padding-top", "15px")

                Dim child() As DataRow = data.Tables(1).Select("Unit = '" & row("Name") & "'")
                If child.Length > 0 Then
                    For i As Integer = 1 To 9
                        'for the 9 columns in the report - way less code to name them like this
                        Dim lbl As Label = CType(.Cells(i).FindControl("lbl" & i), Label)
                        If Not IsDBNull(child(0)(i)) Then
                            lbl.Text = CDec(child(0)(i)).ToString("F1")
                        Else
                            lbl.Text = "0"
                        End If
                        If row("Name") = "TOTAL" Then lbl.Style.Add("padding-top", "15px")
                    Next
                End If

            End With


        ElseIf e.Row.RowType = DataControlRowType.Footer Then

            With e.Row

     
                'this is why the second table columns needed to be typed
                'otherwise, the compute functions do not work
                Dim safety As Decimal = Math.Round(CDec(data.Tables(1).Compute("SUM(Safety) / SUM(Shifts)", "Unit <> 'Total'")), 2)
                Dim one As Decimal = Math.Round(CDec(data.Tables(1).Compute("SUM(One) / SUM(Shifts)", "Unit <> 'Total'")), 2)
                Dim dt As Decimal = 1 - safety - one
                CType(.Cells(7).FindControl("lbl7T"), Label).Text = safety.ToString("P0")
                CType(.Cells(8).FindControl("lbl8T"), Label).Text = one.ToString("P0")
                CType(.Cells(9).FindControl("lbl9T"), Label).Text = dt.ToString("P0")

            End With

        End If

    End Sub
0
NickMalloy
Asked:
NickMalloy
1 Solution
 
VBRocksCommented:
You should be able to do this:

        Dim safety As Decimal
        Dim value As Object = data.Tables(1).Compute("SUM(Safety) / SUM(Shifts)", "Unit <> 'Total'")
        If value IsNot DBNull.Value Then
            safety = Math.Round(CDec(value), 2)
        End If


0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now