0

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)
8
  • What is the actual strinf value of elements in the array? Is it actual "Yes" and "No" ? Commented Sep 26, 2013 at 2:50
  • while storing I am storing boolean values true and false , Commented Sep 26, 2013 at 2:52
  • If they're boolean, why are u storing them in a string array? Commented Sep 26, 2013 at 2:52
  • Actually I am storing whole fetched row in that string array,and now i want to compare that particular value,from that array Commented Sep 26, 2013 at 2:54
  • Ok and can you give an example of actual string values of the array elements? e.g. in your example is editDrData[15] actually "true" or "false" ? Commented Sep 26, 2013 at 2:56

6 Answers 6

1

You need to do a string comparison

bool value = editDrData[15] == "Yes";
Sign up to request clarification or add additional context in comments.

2 Comments

while storing I am storing boolean values true and false ,and not a string
@Durga In the answer above, value will be true or false.
1

String comparison for your table row storing YES/NO.

bool value;
if (editDrData[15] == "Yes")
{
    value = true;
}
else
{
    value = false;
}

Comments

0

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") {

}

5 Comments

but than how I do check whether value is true or false?
@ Yuriy Galanter its showing me this error when i tried it Error Operator '==' cannot be applied to operands of type 'string' and 'bool'
this worked if ((editDrData[15] == "True")Please update your answer
As @Durga said: She is storing the value in string array. How can one store actual boolean in string array. One can store only string version of boolean like "true". So "if ((editDrData[15])" will not compile.
@Durga I've updated my answer after confirming content of the array.
0

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
}

Comments

0

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?

Comments

0

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.