Both are grammatical, but they do mean different things.
When specifies the time of the dream. You had a dream at the time you were walking, which is unlikely but possible.
Where means “in which”, so in this case it serves to specify the content of the dream.
1 at, in, or to which (used after reference to a place or situation):
I first saw him in Paris, where I lived in the early sixties
Although the example given isn’t exactly analogous, the definition (“in which, used after a reference to a situation”) is exactly relevant.
“Once, I had a dream in which I was walking...”
[Note the use of once to indicate a non-specific time in the past, not “one time”]