Database Null problem

MacRaider4

Member
Joined
Jan 8, 2010
Messages
13
Programming Experience
Beginner
I'm working on a program where the user is going to be entering information to a table in Step 1, and then in Step 2 go back and add/update/edit that information. There are fields that are always going to be blank because that work has not been done yet, and I can't figure out how to get my program to stop blowing up with the nulls... This is what I have tried.
VB.NET:
If dsFrank.Tables("Franklin").Rows(r).Item(2).Equals(System.DBNull.Value) Then fCapture = "" Else 
        fCapture = dsFrank.Tables("Franklin").Rows(r).Item(2)

I'm getting InvalidCastException was unhandled Conversion from type 'DBNull' to type String is not valid... though this is going off on the section of code after the Else which it shouldn't be going to in the first place.

This section is where the user clicks on the datagrid showing the info and it places that information into text boxes on the bottom of the form.
 
How about:
VB.NET:
fCapture = Convert.ToString(dsFrank.Tables("Franklin").Rows(r).Item(2))
The Convert.ToString() returns "" for DBNull.Value automatically.
 
Thanks

I have found that this works

VB.NET:
If IsDBNull(dsFrank.Tables("Franklin").Rows(r).Item(2)) Then
                fCapture = ""
            Else
                fCapture = dsFrank.Tables("Franklin").Rows(r).Item(2)
            End If

but I'll give yours a shot as it's a lot shorter.
 
Back
Top