How to convert "0" and "1" to false and true

Posted by Chris on Stack Overflow See other posts from Stack Overflow or by Chris
Published on 2010-04-15T18:44:17Z Indexed on 2010/04/15 18:53 UTC
Read the original article Hit count: 208

Filed under:
|
|
|
|

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!

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET