Dealing with Nulls and Decimals.

Posted on 2007-10-10
Last Modified: 2008-01-09
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")
                            lbl.Text = "0"
                        End If
                        If row("Name") = "TOTAL" Then lbl.Style.Add("padding-top", "15px")
                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
Question by:NickMalloy
    1 Comment
    LVL 27

    Accepted Solution

    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


    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now