91

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!

Chris
  • 6,001
  • 8
  • 31
  • 56

9 Answers9

160

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.

kemiller2002
  • 107,653
  • 27
  • 187
  • 244
  • 9
    Correct? Check; Concise? Check; Elegant? Check. +1. – Earlz Apr 15 '10 at 18:52
  • 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 '10 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. – Pierre-Alain Vigeant Apr 15 '10 at 19:15
  • 4
    Don't forget to check for nulls: !string.IsNullOrEmpty(returnValue) && (returnValue == "1") – csharpforevermore Dec 13 '13 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 09 '14 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 '20 at 15:02
112

In a single line of code:

bool bVal = Convert.ToBoolean(Convert.ToInt16(returnValue))
Austin Henley
  • 4,566
  • 13
  • 42
  • 74
Chris
  • 1,183
  • 1
  • 7
  • 4
  • 3
    I like your version Chris because as the question stated, lets use boolean instead of comparing strings. – Svet Jun 14 '16 at 14:01
  • I also prefer your version because it conveys the intent more clearly. – BornToCode May 15 '18 at 08:15
  • 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 06 '18 at 10:08
12

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.");
}
Zach Johnson
  • 21,761
  • 6
  • 66
  • 83
  • Nice idea to catch the unrecognized value. Not sure I want to go that way, but still a good idea. – Chris Apr 15 '10 at 19:34
6

You can use that form:

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

Or more simply (thanks to Jurijs Kastanovs):

return returnValue.Equals("1");
Nuno Ribeiro
  • 1,732
  • 2
  • 19
  • 26
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

riffnl
  • 2,773
  • 16
  • 29
2

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

bool boolValue = (returnValue == "1");
Pabinator
  • 1,479
  • 1
  • 20
  • 22
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
user2241289
  • 155
  • 1
  • 12
0
(returnValue != "1" ? false : true);
Steffen Winkler
  • 2,666
  • 2
  • 33
  • 55
Amin AmiriDarban
  • 1,769
  • 4
  • 20
  • 29
-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.

mzonerz
  • 1,070
  • 11
  • 19