How to check for nulls and if null, set to zero?

Crystalgrower

New member
Joined
Oct 1, 2007
Messages
1
Programming Experience
Beginner
First off, hello everyone. Hope everyone is well.

I have a formview with several labels that I would like to sum up. These labels may or may not be null. I was trying to loop through the labels and check for nulls and if true, set the label.text value to 0 (zero). Here is my code:

Dim ctrl As Label
Try
For Each ctrl In Labels
If IsDBNull(ctrl.Text) Then
ctrl.Text = "0"
End If
Next

...Label summing code goes here...

Catch ex As Exception
lblStatus.Text = ex.Message.ToString()
End Try


Labels is a collection of label objects. As would be expected, if there is no data in the text of the label is gives me "Conversion from string "" to type 'Double' is not valid." but why doesn't the loop set the label.text to "0"?

Figured this out. It was pretty stupid but I thought I was checking for nulls when I was looking for en empty string. Changed it to:

If ctrl.Text = "" Then

and that worked like a charm.
 
Last edited:
Back
Top