While having lunch, I was stung by a wasp.
If you want "While" to be followed by a gerund, the subject in the second clause needs to be the person who was "doing the gerund".
Note that "While having lunch, a wasp stung me" would be ambiguous. It suggests that it was the wasp who was having lunch.