Search code examples
asp.netlogical-operators

How to use Logical operator OR " || " in ASP .NET


I've followed this question How to use IF statement in asp.net using C#, but I really can make it out with my code.

if ((txtNome.Value == null) || (txtNome.Value == (""))
{

}

Here is the error

The "||" operator cannot be applied a bool and string operands

I've tried all the possible solutions in the question above, but still not working. Some ideas?

Thank you


Solution

  • Solution 1

    if ((txtNome.Value == null) || (txtNome.Value == ""))
        {
    
        }
    

    Solution 2 The same as above but without the extra round brackets. These are unnecessary for single logical statements.

    if (txtNome.Value == null || txtNome.Value == "")
    {
    
    }
    

    Solution 3 Built in function for the above in C#

    if (string.IsNullOrEmpty(txtNome.Value))
    {
    
    }