Problem OccurredCast from type ‘DBNull’ to type ‘String’ is not valid

I am working on optimizing an application which utilizes an access database. Just before the application finished processing I received and error message, Problem OccurredCast from type ‘DBNull’ to type ‘String’ is not valid.  I’m not sure what is causing this, however I did some research and it appears to be related to null values in the database.  How do fix this problem?
Thank you for taking the time to read this post,
Gr8life
gr8lifeAsked:
Who is Participating?
 
appariCommented:
Try this


                For Each dr As datarow In dtInput.rows
                    If NOT IsDBNull(dr.Item(3) ) Then
                  dr.Item("Lookup1") = iplookup(dr.Item(3))
                ELSE
                  dr.Item("Lookup1") = ""
                    END IF
                    If NOT IsDBNull(dr.Item(5) ) Then
                  dr.Item("Lookup2") = iplookup(dr.Item(5))
                ELSE
                  dr.Item("Lookup2") = ""
                    END IF
                   
                    sOutLine = ""
                    For iCnt As Integer = 0 To 15
                        If IsDBNull(dr.Item(iCnt) ) Then
                               sOutLine &=  ","
                        else
                               sOutLine &= dr.Item(iCnt) & ","
                        end if
                        If iCnt = 3 Then
                            sOutLine &= dr.Item("Lookup1") & ","
                        else If iCnt = 5 Then
                            sOutLine &= dr.Item("Lookup2") & ","
                        End If
                    Next
                   

                    sout.WriteLine(sOutLine)

                    lineCounter = lineCounter + 1
                    If lineCounter Mod 10000 = 0 Then
                        sout.Flush()
                    End If

                Next

0
 
appariCommented:

posted reply in your old question
0
 
gr8lifeAuthor Commented:
I am trying the changed code now.
Thanks,
Gr8life
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
gr8lifeAuthor Commented:
Still got the same error message when I ran the changed code against the 1,000 row data set.
Any suggestions?
Thanks,
Gr8life
0
 
bchoorCommented:
have you tried to check using IsDBNull(Value) ? If so, then you can prevent any interaction with that value.

Also, does it give you which field the error is happening on? Also, what exactly are you doing with the db, are you pulling the values in and peforming some operation on it - if so as I pointed out above, use the IsDBNull...like so

Let's say you have a datarow dr
Dim dr As DataRow = ds.Tables(0).rows(0)
Dim Field1Value As String = ""

If isdbNull(Dr.Item("Field1")) Then
  Field1Value = ""
Else
  Field1Value = Dr.Item("Field1")
End IF

HTH
~BC
0
 
gr8lifeAuthor Commented:
The change worked thank you very much,
Gr8life
0
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.

All Courses

From novice to tech pro — start learning today.