C# problems with Validating String and displaying an error messages when null or empty

StackOverflow https://stackoverflow.com/questions/19296517

  •  30-06-2022
  •  | 
  •  

سؤال

I'm trying to get error messages to show if two textboxes are empty and this stops the form from processing any data if so. The error message shows when the textboxPhoneNum.Text is empty or filled with invalid data.

However it does not show when textboxName.Text is empty and it also processes all the code and data when it should be stopped.

        int Phone;  

        string Name = textboxName.Text;

      if (String.IsNullOrEmpty(Name))
        {
            if (int.TryParse(textboxPhoneNum.Text, out Phone))
            {

               code..

            }

            else
            {
                //comment
                MessageBox.Show("Please fill in name and phone number.");
            }
        }
        else
        {
            //comment
            MessageBox.Show("Please fill in name and phone number.");
        }
    }
هل كانت مفيدة؟

المحلول

Missing negation in that row maybe?

if (String.IsNullOrEmpty(Name))

to

if (!String.IsNullOrEmpty(Name))
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top