Search code examples
databasevb.netstructure

Setting a string to be empty if value is null


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.


Solution

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