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

Handling Nulls in Gridview Summary


I am trying to display the sum of a column in a grdivew. this seems to work fine but if their are nulls in the database then it fails error msg is "Object cannot be cast from DBNull to other types"

How do I get around this

code is attached

thanks in advanced
Dim decCapitalComm As Decimal = 0 
If e.Row.RowType = DataControlRowType.DataRow Then
            decCapitalComm += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Capital_commitment"))
        ElseIf e.Row.RowType = DataControlRowType.Footer Then
            e.Row.Cells(1).Text = "Totals:"
            e.Row.Cells(2).Text = decCapitalComm.ToString '("d")               
            e.Row.Cells(1).HorizontalAlign = HorizontalAlign.Right
            e.Row.Font.Bold = True
        End If

Open in new window

1 Solution
Jorge PaulinoIT Pro/DeveloperCommented:
Try this way

If Not IsNull(DataBinder.Eval(e.Row.DataItem, "Capital_commitment")) Then
   decCapitalComm += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Capital_commitment"))
End If
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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