Require data in at least one text box on the search page

I am trying to get the user to enter data in at least one of five text fields before running a search query ..

I am getting error messages

  • converting string to boolean 2 "Operator" & cannot be applied to operands of type 'string' and 'string' "

    if (txtLoginName.Text=String.Empty && 
        txtDisplayName.Text = String.Empty && 
        txtCode.Text = String.Empty && 
        txtEmailAddress.Text = String.Empty && 
        txtName.Text = String.Empty)
    {
    
        lblErrorMessage.Text = "At least one search criteria is required.";
        return;
    }
    
          

+2


source to share


3 answers


Try the below code. In your example, you used "=" instead of "==" in C #.

   if (txtLoginName.Text==String.Empty && 
        txtDisplayName.Text == String.Empty && 
        txtCode.Text == String.Empty && 
        txtEmailAddress.Text == String.Empty && 
        txtName.Text == String.Empty)

      



Another way to accomplish the same thing would be this:

   if (!String.IsNullorEmpty(txtLoginName.Text) && 
        !String.IsNullorEmpty(txtDisplayName.Text) && 
        !String.IsNullorEmpty(txtCode.Text) && 
        !String.IsNullorEmpty(txtEmailAddress.Text) && 
        !String.IsNullorEmpty(txtName.Text))

      

+8


source


I'm trying to get used to using String.IsNullOrEmpty () instead of direct comparison. While it does the same thing under the hood (AFAIK), it is good discipline when you are not working with text fields but string values ​​that might be empty.

if (String.IsNullOrEmpty(txtLoginName.Text) && 
    String.IsNullOrEmpty(txtDisplayName.Text) && 
    String.IsNullOrEmpty(txtCode.Text) && 
    String.IsNullOrEmpty(txtEmailAddress.Text) && 
    String.IsNullOrEmpty(txtName.Text))
{
}

      



</ 2c>

+1


source


The brackets around each test will also clarify, for example

if ( (txtLoginName.Text == String.Empty) &&
     (txtDisplayName.Text == String.Empty) )
{
}

      

or

if ( (String.IsNullOrEmpty(txtLoginName.Text)) &&
     (String.IsNullOrEmpty(txtDisplayName.Text)) )
{
}

      

0


source







All Articles