Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to test if a String is DBnull

I’m trying to find how to test if a String, from a database, is null or not.
Unfortunately, whatever i’m trying, i always find the same exception :
Conversion from type ‘DBNull’ to type ‘String’ is not valid.

CuttingLength1 is a String property into TypedDataRow

Dim dsResults As TypedDataSet
Dim dtResults As TypedDataSet.TypedDataTable
Dim result As TypedDataSet.TypedDataTable.TypedDataRow
Using data As New Bl.Method(MetaData) 
    dsResults = data.GetData(parameter)
    dtResults = dsResults.Typed
    result = dtResults.First
    If (Not result Is Nothing) Then
        If Not result.CuttingLength1 Is Nothing Then
            'If Not (DBNull.Value.Equals(result.CuttingLength1)) Then
            'If Not IsDBNull(result.CuttingLength1) Then
            TextBoxM1.Text = result.CuttingLength1
        End If
    End If
End Using

I’ve also tried with expressions in captions

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You are using a typed DataSet, so every typed DataRow has a dedicated method for each column to determine whether it is NULL or not. Your code should look something like this:

If Not result.IsCuttingLength1Null() Then
    TextBoxM1.Text = result.CuttingLength1
End If

You don’t work directly with DBNull in a typed DataSet.

I think that’s what the method will be named but, if not, it will be similar and Intellisense will show you. It may also be a property but I think it’s a method. There is also a corresponding SetCuttingLength1Null method.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading