Avatar of cdemott33
cdemott33
Flag for United States of America asked on

Help solving error: "Conversion from type 'DBNull' to type 'String' is not valid."

Hi Folks - I am getting an error in my procedure below and I'm hoping you can help me fix the problem.  

I have BLL Function that returns a datatable.  Some of the rows returned from my datatable may not have a value, so it returns DBNull.  How can I avoid the "DNull to type 'String' error when there is no value returned?

Please see my code below.
Dim customerName As String
        Dim customerAddrOne As String
        Dim customerAddrTwo As String
        Dim customerAddrThree As String

        Dim dt As DataTable = clientLogic.GetClientInfo(ClientID)

        If dt.Rows.Count > 0 Then
            customerName = dt.Rows(0).Item("SOLDTO")
            customerAddrOne = dt.Rows(0).Item("SOLD1")
            customerAddrTwo = dt.Rows(0).Item("SOLD2")
            customerAddrThree = dt.Rows(0).Item("SOLD3")
        End If

Open in new window

ASP.NETVisual Basic.NETC#

Avatar of undefined
Last Comment
cdemott33

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
disrupt

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Paul Jackson

Are you using a stored procedure to return the results you would be best ensuring that no nulls are returned by the stored procedure :

Select ISNULL(mycol, '') as myCol
From mytable

Otherwise check whether it is null before assigning to string:

If dt.Rows.Count > 0 Then 
     If Not IsDBNull(dt.Rows(0).Item("SOLDTO")) Then
            customerName = dt.Rows(0).Item("SOLDTO")
     End If 
     If Not IsDBNull(dt.Rows(0).Item("SOLD1")) Then
            customerAddrOne = dt.Rows(0).Item("SOLD1") 
      End If
      If Not IsDBNull(dt.Rows(0).Item("SOLD2")) Then
            customerAddrTwo = dt.Rows(0).Item("SOLD2") 
      End If
      If Not IsDBNull(dt.Rows(0).Item("SOLD3")) Then
            customerAddrThree = dt.Rows(0).Item("SOLD3") 
      End If
End If

Open in new window

disrupt

you can also do:

IsDBNull(dt.Rows(0).Item("SOLDTO")
IsDBNull(dt.Rows(0).Item("SOLD1"))
IsDBNull(dt.Rows(0).Item("SOLD2"))
IsDBNull(dt.Rows(0).Item("SOLD3"))
disrupt

IsDBNull(dt.Rows(0).Item("SOLDTO")) = False ? dt.Rows(0).Item("SOLDTO") : ""
IsDBNull(dt.Rows(0).Item("SOLD1")) = False ? dt.Rows(0).Item("SOLD1") : ""

...

...
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
cdemott33

ASKER
Thanks!