Error whith null value

Qbert

Active member
Joined
Jun 11, 2007
Messages
33
Location
Minnesota
Programming Experience
Beginner
hello

i am getting an error when i am selecting from a table and there is a null value in the table.

Conversion from type 'DBNull' to type 'String' is not valid.

is there a way to prevent this? i tried this but i get errors with this.
VB.NET:
If (not dr.IsDBNull(4)) Then
                ClientAddress2TextBox.Text = dr.Item(4)
            End If

thanks
 
never mind i got it. i did this and it worked

VB.NET:
If dr.Item(4).ToString = "" Then
                'do nothing
            Else
                ClientAddress2TextBox.Text = dr.Item(4)
            End If
 
if item 4 is, for example, your address_line_1 then you would say:

If Not myDataTable.IsADDRESS_LINE_1Null() Then
...
EndIf



Try to get out of the habit of working with datareaders, column ordinals rather than names, and performing empty If clauses for the nsake of putting a NOT into the if clause (turn the logic over)

Your code would look more like:

If dr(4) <> DBNull.Value Then
...
 
Back
Top