How to catch DBNulls from .Net SQLRows

There’s plenty of discussion on this out there. A Try..Catch block worked best for me. Since the error is thrown from the SQLRow innards, Convert.IsDBNull etc does not work.

Dim strRes As String = ""
            Dim a As New SqlDataAdapter("SELECT * FROM CMN_Settings WHERE SettingKey = ‘" & pKey & "’", Config.GetConnectionString("SiteSQLServer"))
            Dim d As New DSCommonEntities.CMN_SettingsDataTable
            If d.Rows.Count > 0 Then
                Dim r As DSCommonEntities.CMN_SettingsRow
                r = d.Rows(0)
                    strRes = r.setingValue
                Catch e As Exception
                    strRes = ""
                End Try
            End If
            Return strRes

Powered by ScribeFire.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s