> IIRC - ExecuteScalar returns null if no value is returned, and
> DBNull.Value if a null is returned. So test the value for these two
> cases before going further. You could also try using
> Convert.ToString(...) which handles null (don't know about DBNull).
Using Convert.ToString() is rather pointless, since Convert.ToString(null)
returns null (the reason for this is the rather interesting overload
resolution that C# uses) while Convert.ToString(DBNull.Value) returns the
empty string. It's unlikely this is what you want, and even if it is,
readers of your code are probably not going to grasp immediately what's
happening. Better to test explicitly.
> Note also that it is more reliable to use "using" than Dispose().
To be precise, "using" just calls Dispose(), but in a finally block.

Signature
J.
Marc Gravell - 26 Mar 2008 21:57 GMT
IIRC it also handles the scenario when the disposable object is
initialized as null.
But yes - getting disposed on exception is the point I meant by "more
reliable".
Marc
Anodes - 27 Mar 2008 15:10 GMT
> IIRC it also handles the scenario when the disposable object is
> initialized asnull.
[quoted text clipped - 3 lines]
>
> Marc
Final working code:
object oScalarReturned = new Object();
oScalarReturned = command.ExecuteScalar();
if (oScalarReturned is DBNull || oScalarReturned == null)
scalarReturned = "";
else
scalarReturned = oScalarReturned.ToString();