0

I have an if/else statement that is not working.

while (rdr.Read())
{
  string permission = rdr["Permission"].ToString();
  if (permission == "Exec")
  {
     Run my code
  }
  else
  {
     lblErrorStart.Visible = true;
  }
}

If Permission does equal Exec then everything works fine but (when stepping through the code) I have noticed that when Permission does not equal Exec, it does not trigger the Else. It just goes back to the while statement and stops. Let me know if I need to provide any more code.

Note: I only have Exec in the database. Everything else is null.

10
  • Try getting rid of the white-space in between the if statement's closing bracket and the else statement? Commented Jun 20, 2011 at 20:43
  • 12
    @MoarCodePlz: Are you serious? Commented Jun 20, 2011 at 20:46
  • 2
    Do a rebuild of the entire solution and retest. It could be that the source code is out-of-sync with the binaries. Commented Jun 20, 2011 at 20:48
  • @MoarCodePlz: C# is not white space or indent sensitive (there are some exceptions, such as concatenating a string across newlines, but in this case it doesn't matter). Commented Jun 20, 2011 at 20:48
  • 2
    string permission = rdr["Permission"].ToString(); will throw a NullReferenceException if rdr["Permission"] is null. Commented Jun 20, 2011 at 20:49

3 Answers 3

4

I have noticed that when Permission does not equal Exec, it does not trigger the Else.

I have a very hard time believing that. Please show us the exact contents of permission when it does not equal "Exec".

Also realize that setting the label to visible will not update as soon as that code is executed. This is because you are not allowing the Windows Message Loop to process messages. So even though the Visible property is set to true, a WM_PAINT message is never processed (until your loop exits), so the appearance of your control will not change.

EDIT:

As Brian Gideon points out in a comment, your executable version may be out of sync with your code. Rebuild the entire project and try it again.

Sign up to request clarification or add additional context in comments.

Comments

1

Sometimes when testing exact equality, you fail based on what you do NOT see... If your data is from a record set, or other structure and the actual value is NOT trimmed(), it will fail...

"Exec " == "Exec" will fail

Try

string permission = rdr["Permission"].ToString().Trim();

Comments

0

Basically it's an if else statement like stated:

if(label1.Text == "True")
{
    label1.Forecolor = Color.Green;
}
else
{
    label1.Text = "ERROR!";
    label1.Forecolor = Color.Red;
}

Also, you can do multiple if statements and if none of them are relatively true you can have them all lead to the else statement.

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.