105

I have a method which is connecting to a database via Odbc. The stored procedure which I'm calling has a return value which from the database side is a 'Char'. Right now I'm grabbing that return value as a string and using it in a simple if statement. I really don't like the idea of comparing a string like this when only two values can come back from the database, 0 and 1.

OdbcCommand fetchCommand = new OdbcCommand(storedProc, conn);

fetchCommand.CommandType = CommandType.StoredProcedure;
fetchCommand.Parameters.AddWithValue("@column ", myCustomParameter);
fetchCommand.Parameters.Add("@myReturnValue", OdbcType.Char, 1)
            .Direction = ParameterDirection.Output;
fetchCommand.ExecuteNonQuery();

string returnValue = fetchCommand.Parameters["@myReturnValue"].Value.ToString();
if (returnValue == "1")
{
    return true;
} 

What would be the proper way to handle this situation. I've tried 'Convert.ToBoolean()' which seemed like the obvious answer but I ran into the 'String was not recognized as a valid Boolean. ' exception being thrown. Am I missing something here, or is there another way to make '1' and '0' act like true and false?

Thanks!

9 Answers 9

184

How about:

return (returnValue == "1");

or as suggested below:

return (returnValue != "0");

The correct one will depend on what you are looking for as a success result.

6
  • 12
    I would recommend using return (returnValue!="0"). It would be more natural that 0 is false and every number that is not zero is true. Of course here we have the case where Chris uses strings instead of numbers, so this comment is only partly valid ;)
    – Gacek
    Apr 15, 2010 at 18:54
  • It's always a debate. 0 also mean ERROR_SUCCESS which mean that everything went well. But I agree with Gacek that it is more natural. Apr 15, 2010 at 19:15
  • 4
    Don't forget to check for nulls: !string.IsNullOrEmpty(returnValue) && (returnValue == "1") Dec 13, 2013 at 16:27
  • 2
    why not Convert.ToBoolean(1)? It does the same and you are using the framework for the check. I like the answer above as well, but which one is better to be used?
    – user20358
    Oct 9, 2014 at 11:28
  • I'm currently struggling with dbnull values (that we guess. should be false too)... if you're unlucky enough to have a table where nulls are all of a sudden allowed... :|
    – Erk
    Nov 11, 2020 at 15:02
135

In a single line of code:

bool bVal = Convert.ToBoolean(Convert.ToInt16(returnValue))
4
  • 3
    I like your version Chris because as the question stated, lets use boolean instead of comparing strings.
    – Svet
    Jun 14, 2016 at 14:01
  • I also prefer your version because it conveys the intent more clearly.
    – BornToCode
    May 15, 2018 at 8:15
  • 1
    It's proper only for "1" or "0". For any other string return value is not deterministic, for example "101" is true and so on...
    – szubajak
    Aug 6, 2018 at 10:08
  • @szubajak I don't understand your comment. Sure it is deterministic as only 0 is false as boolean. Aug 31 at 8:42
14

If you want the conversion to always succeed, probably the best way to convert the string would be to consider "1" as true and anything else as false (as Kevin does). If you wanted the conversion to fail if anything other than "1" or "0" is returned, then the following would suffice (you could put it in a helper method):

if (returnValue == "1")
{
    return true;
}
else if (returnValue == "0")
{
    return false;
}
else
{
    throw new FormatException("The string is not a recognized as a valid boolean value.");
}
2
  • Nice idea to catch the unrecognized value. Not sure I want to go that way, but still a good idea.
    – Chris
    Apr 15, 2010 at 19:34
  • Very easy and simple trick. Apr 25 at 12:54
9

You can use that form:

return returnValue.Equals("1") ? true : false;

Or more simply (thanks to Jurijs Kastanovs):

return returnValue.Equals("1");
2
  • 9
    Just lose the "? true : false" bit. It's totally unnecessary. Leave it at { return returnValue.Equals("1") } May 31, 2016 at 10:23
  • I would check for false. Every number that's not 0 usually is true. Aug 31 at 8:45
5

Set return type to numeric - you don't need a char (so don't use it); a numeric value (0/1) can be converted with Convert.ToBoolean(num)

Otherwise: use Kevin's answer

2
  • I wish that we could change the return type. But we're stuck with what it is.
    – Chris
    Apr 15, 2010 at 19:32
  • 10
    Convert.ToBoolean accepts only True/true/False/false strings
    – Yaro
    Sep 16, 2013 at 6:19
2

Or if the Boolean value is not been returned, you can do something like this:

bool boolValue = (returnValue == "1");
1

My solution (vb.net):

Private Function ConvertToBoolean(p1 As Object) As Boolean
    If p1 Is Nothing Then Return False
    If IsDBNull(p1) Then Return False
    If p1.ToString = "1" Then Return True
    If p1.ToString.ToLower = "true" Then Return True
    Return False
End Function
0
(returnValue != "1" ? false : true);
0
-1

If you don't want to convert.Just use;

 bool _status = status == "1" ? true : false;

Perhaps you will return the values as you want.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.