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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

appariCommented:

posted reply in your old question
gr8lifeAuthor Commented:
I am trying the changed code now.
Thanks,
Gr8life
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
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gr8lifeAuthor Commented:
The change worked thank you very much,
Gr8life
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.