Nate Pet
Nate Pet

Reputation: 46222

C# check of 2 text boxes have data in them

I like to check if 2 of the textboxes have data in them. If so, I would like to do some processing.

I have the following code but not sure why it executes even when one of the textboxes is empty:

     if (!(string.IsNullOrWhiteSpace(txtUp.Text) && (string.IsNullOrWhiteSpace(txtLower.Text)))) 
     {
        // Go here only if both textboxes have data in them

     }

Upvotes: 0

Views: 46

Answers (2)

Rahul Tripathi
Rahul Tripathi

Reputation: 172398

You may try this ie, you are missing to put both the condition in brackets() as you want to check both and then ! them:-

if (!((string.IsNullOrWhiteSpace(txtUp.Text) && (string.IsNullOrWhiteSpace(txtLower.Text)))))

Upvotes: 2

Johan Hjalmarsson
Johan Hjalmarsson

Reputation: 3493

You are missing a ! in your seccond condition. Should be:

                                              here
                                              v
if (!string.IsNullOrWhiteSpace(txtUp.Text) && !string.IsNullOrWhiteSpace(txtLower.Text)) 
{
   // Go here only if both textboxes have data in them
}

Also, the parentheses were messed up

Upvotes: 3

Related Questions