I am returning a scalar value from a SQL Server 2008 database:
string reason = cmd.ExecuteScalar().ToString() ?? : "";
I want to make sure that if null is returned, that reason = "" and not null.
i am getting an error on this line:
Error 3 Invalid expression term ‘:’
How can this be fixed?
EDIT:
thank you for the changes on the colon, now i am getting this exception on the same line:
string reason = cmd.ExecuteScalar().ToString() ?? "";
System.NullReferenceException occurred Message="Object reference not set to an instance of an object."
Try this:
BUT: this will still fail, since if
.ExecuteScalar()returns aNULL, you’re already causing a Null Reference Exception by calling.ToString()on that NULL value……So I guess the
??operator really doesn’t help you here… do the “usual” dance: