We help IT Professionals succeed at work.

Dealing with Nulls and Decimals.

NickMalloy
NickMalloy asked
on
622 Views
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")
                        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
Comment
Watch Question

Top Expert 2007
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.