1

I would like to test SQL output parameters for NULL values. What I have realised is that SQLString.NULL is not equal to DBNull.Value. My question is, is there a more general way of checking a parameter for null values?

Ideally I do not want to have to check each parameter based on their SQL type, e.g. integer parameters SqlTypes.SqlInt32.Null.

Here is a snippet of my code:

.ExecuteStoredProcedure("spGetClientDetails")

If CInt(.Parameters("@return_val").Value) = 0 Then

If myParams(1).SqlValue.Equals(SqlTypes.SqlString.Null) Or myParams(1).SqlValue.Equals(System.DBNull.Value) Then
    Claimants = ""
Else...

1 Answer 1

2

If you use Value instead of SqlValue, you'll get regular .NET types instead of an SqlType. When the database returns NULL, Value will be set to DBNull.Value.

A concise way to check for DBNull.Value is TryCast:

Dim Claimants As String = TryCast(myParams(1).Value, String)

This will set Claimants to Nothing if myParams(1) is not a string. To set Claimants to an empty string instead, you can add If:

Dim Claimants As String = If(TryCast(myParams(1).Value, String), "")
Sign up to request clarification or add additional context in comments.

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.