• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 261
  • Last Modified:

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
0
gr8life
Asked:
gr8life
  • 3
  • 2
1 Solution
 
appariCommented:

posted reply in your old question
0
 
gr8lifeAuthor Commented:
I am trying the changed code now.
Thanks,
Gr8life
0
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now