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.

Nix
  • 57,072
  • 29
  • 149
  • 198
Matt
  • 1,220
  • 3
  • 21
  • 36
  • Try getting rid of the white-space in between the if statement's closing bracket and the else statement? – MoarCodePlz Jun 20 '11 at 20:43
  • 12
    @MoarCodePlz: Are you serious? – BoltClock Jun 20 '11 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. – Brian Gideon Jun 20 '11 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). – Ed S. Jun 20 '11 at 20:48
  • Are you sure `else` is not triggered? Or could it be that you are doing something already done? If you put `MessageBox.Show("Hello");` in the statement, does it show a messagebox? – KilZone Jun 20 '11 at 20:48
  • 2
    `string permission = rdr["Permission"].ToString();` will throw a `NullReferenceException` if `rdr["Permission"]` is null. – mihi Jun 20 '11 at 20:49
  • @mihi great catch. I didn't even think about that. – Matt Jun 20 '11 at 20:58
  • As Ed S. mentions below, the control visibility won't change right away, do you have a break point on the code in the `else` to see if it's executing or not? – QuinnG Jun 20 '11 at 21:00
  • mihi, can you put that as the answer so I can give you credit? – Matt Jun 20 '11 at 21:01
  • FYI, I added another value into the database and the Label triggered as expected. – Matt Jun 20 '11 at 21:04

3 Answers3

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.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
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();

DRapp
  • 47,638
  • 12
  • 72
  • 142
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.

Joe Mayo
  • 7,501
  • 7
  • 41
  • 60
gbgjbj
  • 1