You can do everything right and still get sick. The human gut is a sensitive ecosystem. If you live in a sterile, highly sanitized Western environment, your body has never seen E. coli strains that an Indian local has built immunity to since childhood.
If you are unlucky enough to get Delhi Belly, your goal is to prevent dehydration while letting your body clear the infection. delhi-belly