Has Bruce Banner ever transformed into the Hulk due to a nightmare or bad dream, and if he has, how does he stop himself from doing so every time he has a bad dream?
If memory serves, his ultimate resolution was that his sleep-hulking was being caused by his attempts to use hypnosis to "cage" the Hulk inside his mind. Once he stopped this treatment, his condition returned to normal.
Here's an example from the comics:
Defenders Volume 1, Issue 21, March 1975
The Hulk was part of a loose group called the Defenders for years. The group was led by Dr. Strange.
In this issue, Dr. Banner arrives at Strange's Sanctum Sanctorum, after recently waking up in a nearby alley:
Strange is nice enough to put him up in a bed:
But, across town, Gorilla Man injects some weird serum into Chondu the Mystic's brain, causing mystical nightmares across the city:
Unfortunately for the Defenders (and everyone else), Banner happens to be one of those "sleeping minds":
Valkyrie bravely (foolishly) tries to stop the Hulk. Luckily, Chondu passes out before Hulk finishes smashing her:
As to the second part of your question, "how does he stop himself from doing so every time he has a bad dream", this was not a normal dream. I'm working on reading all the Hulk comics, and I don't think I've seen him transform from a regular dream yet, but if I do I'll be sure to edit this answer.