Reputation: 1303
In an Access database, I have a column named Display
which is of type Yes/No
. I am fetching a table row and storing it in a string array named editDrData[]
. Now, I want to check whether it is true or false. I tried the following, but it's not working. What is the right way to do this check?
if (Convert.ToBoolean(editDrData[15]) == false)
Upvotes: 0
Views: 1211
Reputation: 1042
The string must equal (System.Boolean.TrueString) "TRUE" or (System.Boolean.FalseString) "FALSE", to use Convert.ToBoolean(string) if you are going to use such function all over the project you can override several method and make your own converter
http://msdn.microsoft.com/en-us/library/ayybcxe5.aspx
Upvotes: 0
Reputation: 39777
If, according to you, array actually holds boolean values, you don't have to do any conversion. Simple
if (editDrData[15]) {
}
will do the trick. If its indeed strings, this should work;
if (editDrData[15] == "True") {
}
Upvotes: 0
Reputation:
String comparison for your table row storing YES/NO.
bool value;
if (editDrData[15] == "Yes")
{
value = true;
}
else
{
value = false;
}
Upvotes: 1
Reputation: 3756
in .net if you want to pass string to Convert.ToBoolean(), it can only be "True" or "False"
Or I think it will throw exception.
Have you check that is the actual value stored in the database as string? If it is not True and False, you cannot do that in this way.
You can just compare the string in this case
e.g.
if (string.Equals(editDrData[15], "YES???", StringComparison.CurrentCultureIgnoreCase)) { }
String.
BTW: would that be better to store that as boolean when importing into the system?
Upvotes: 0
Reputation: 2095
It should work. I'd say it is something wrong with the formatting of the string or the logic. You'll need to insert a breakpoint to check but if it conforms to the examples from microsoft then the string should convert succesfully... (if you do put a breakpoint in and find out the value at run time then you will get better answers from us)
So if editDrData[15] looks like "true" or "false"(case doesn't matter) then the code you have above will work.
If the string you have at editDrData[15] is actually "Yes" or "No" then you cannot use Convert.Boolean(string); and you will have to put your own logic in like
if(editDrData[15] == "No")
{
//do stuff here
}
Upvotes: 0
Reputation: 1219
You need to do a string comparison
bool value = editDrData[15] == "Yes";
Upvotes: 1