Consider this:
I have a user-defined stored procedure which returns a bit value as OUTPUT.
In my calling code I want to check that value.
I define it in my code as follows:

Dim retval As New SqlParameter()
retval.ParameterName = "@return"
retval.SqlDbType = SqlDbType.Bit
retval.Direction = ParameterDirection.Output
DataCommand.Parameters.Add(retval)
DataCommand.ExecuteNonQuery()
' now check the return value
If Not (CBool(retval.Value)) Then
    Throw New ApplicationException("SQL returned FALSE. ")
End If

Which appears to get the desired results, in that my code hits the If statement and continues.
However, if I change the If statement to
If Not (CBool(retval.SqlValue)) ...
I get an exception
Conversion from type 'SqlBoolean' to type 'Boolean' is not valid
How should I be handling this? Insight, comments please.
Thanks

Recommended Answers

retVal.Value returns 0 or 1 to bit columns, so converting to bool works. However, retVal.SqlValue returns an SqlBoolean object, that can't be converted to a bool, but the SqlBoolean object has an Value property that is a bool.

So, both should work:

CBoll(retval.Value)
'And
CType(retval.SqlValue, 'SqlBoolean').Value

I don't know if …

Jump to Post

Did you import System.Data.SqlTypes ?

Jump to Post

All 4 Replies

retVal.Value returns 0 or 1 to bit columns, so converting to bool works. However, retVal.SqlValue returns an SqlBoolean object, that can't be converted to a bool, but the SqlBoolean object has an Value property that is a bool.

So, both should work:

CBoll(retval.Value)
'And
CType(retval.SqlValue, 'SqlBoolean').Value

I don't know if the syntax are ok cause I don't code much in VB. But you'll get the idea.

Thanks for the feedback.
I couldn't get the CType to work, SQLBoolean wasn't a valid object type, so I tried Boolean again, and that threw an invalid cast exception. Barring any enlightening insight from the crew, I guess I'll stick with CBool(retval.Value) - which same, if my logic is working, exposes that SqlBoolean value you refer to...

What drives me batty is that I can step through with the debugger and see that retval.SqlValue has a default property of {True}

Did you import System.Data.SqlTypes ?

Imports System.Data.SqlTypes made no difference, but in debugging I could see the Value property, which was not an available property in Intellisense. It turns out that SqlValue.Value is a late binding property.

I can only use If(CBool(retval.SqlValue.Value)) ... (and I still have to use CBool() because of my compiler settings) if I disable my late binding warning in Project Properties -> Warning Configurations -> Late binding; call could fail at run time.

I run Option Strict though, so I guess I can live with using the retval.Value object (and explicit type casting!) instead :)

Thanks for the help.