1

I am not sure how to search for the issue I am trying to solve here. In the program I am writing (in VB.Net) I am trying to assign values pulled from a database to different variables in a structure.

Now my issue is that sometimes, some of the values pulled from the database are NULL, for example not every phone number has an extension. This is what I have for my code at the moment:

Structure CustomerContact
    Public _name As String
    Public _email As String
    Public _fax As String
    Public _phone1 As String
    Public _phone2 As String
    Public _phone3 As String
    Public _ext1 As String
    Public _ext2 As String
    Public _ext3 As String
    Public _type1 As String
    Public _type2 As String
    Public _type3 As String
End Structure

    Dim contactData As DataTable = CustomerDBFunctions.GetCustomerContacts(Customer)
    For Each row As DataRow In contactData.Rows
        If contacts.Count < 1 Then
            contacts.Add(New CustomerContact With {
                            ._name = row.Item("FullName").ToString() & " (" & row.Item("ContactType").ToString() & ")",
                            ._email = row.Item("Email").ToString(),
                            ._fax = row.Item("Fax").ToString(),
                            ._phone1 = row.Item("Phone").ToString(),
                            ._ext1 = row.Item("Extension").ToString(),
                            ._type1 = row.Item("PhoneType").ToString()})
        End If
    Next

Right now I am getting an error when the value in the database is NULL because it can't assign a NULL value to a string. I'd like to in the instances where a NULL value is present instead set the value of the variable to "" instead. I am just unsure how to code this.

3
  • Here's a similar question: stackoverflow.com/q/222834/5162073 Commented Oct 26, 2018 at 12:10
  • What exception are you seeing because nulls should not be causing an issue there. Database nulls are represented by DBNull.Value and calling ToString on that should return an empty String. That means that row.Item("Extension").ToString() should already be returning an empty String if that field is null. Commented Oct 26, 2018 at 12:14
  • By the way, that structure is too big. It should be a class. Class or structure, you should also be using properties rather than fields and not using names like _name rather than Name. Commented Oct 26, 2018 at 12:14

2 Answers 2

3

Technically, the problem isn't that the column is null. String is a reference type, so it can but null (though, if it was null, you wouldn't be able to call ToString on it anyway). What's actually going on is that ADO.NET always returns DBNull.Value for all columns where the row contains a null value.

You could check it, like this:

If row.Item("Phone") <> DBNull.Value Then
    customerContact._phone1 = row.Item("Phone")?.ToString()
End If

Note, I used ?. instead of . when calling ToString just in case the column actually is null rather than DbNull.Value. The reason for this is that I don't know what kind of code you're using to fill that DataTable. If it's ADO.NET that's filling it, it'll never be null, but if it's custom code that populates it via some other means, it might get actual nulls in it.

Since you are using a DataTable to return the value, it has a convenient IsNull method that you can use, which cleans up the code a little bit:

If Not row.IsNull("Phone") Then
    customerContact._phone1 = row.Item("Phone")?.ToString()
End If

Obviously, if you're doing this a lot, it would be good to wrap it up into a reusable function. Also, if you want to shorten it up into one line, you could do so like this:

._phone1 = If(row.IsNull("Phone"), row.Item("Phone")?.ToString(), Nothing)
Sign up to request clarification or add additional context in comments.

Comments

1

String concatenation can be used to convert Nothing to "" (other alternatives in my answer here)

._fax = row!Fax & "",

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.