IF IF Else doesn't work as I need and want

I need to know how I could handle my problem in a efficient and smart way:

I want an error-handling for user's input; I have 2 fields CustomerID and Company:

if (customerIDExists)
{
    errorMessage = "...";
}
if (companyInvalid)
{
    errorMessage = "...";
}
else
{
    // Add user to DB
}

There could be 3 errors:

  1. CustomerID already exists
  2. Company has an invalid name
  3. CustomerID AND Company is invalid at the same time

With the example above the else is for the last if (companyInvalid). How can I deal with the else that this only executes when the 1 and 2 if doesn't match?

Else if is not right because I have to check all opportunities.

Jon Skeet
people
quotationmark

Well, you could make it very explicit:

if (customerIDExists)
{
    ...
}
if (companyInvalid)
{
    ...
}

if (!customerIDExists && !companyInvalid)
{
    // Add user to DB
}

Or just use the fact that you haven't got an error message:

string errorMessage = null;
if (customerIDExists)
{
    errorMessage = ...;
}
if (companyInvalid)
{
    errorMessage = ...; // Don't forget it could already be non-null!
}

if (errorMessage == null) 
{
    // No error message, so we must be fine
    // Add user to DB
}

people

See more on this question at Stackoverflow