The term modulation doesn’t apply here, this is simply a chord progression. A modulation is when there is a full blown change of key in a song or piece. When you talk about sounding “off” I assume you’re referring to something that sounds outside of the given chord, more often called “playing out” or “outside”, correct me if I’m wrong. Considering you’re just starting out and this is a very simple chord progression I suggest sticking to ideas and phrases that work within the harmony for now and trying to come up with creative ideas that way.
This chord progression gives you many options for playing interesting solos. First you have B major 5 sharps, and then D major, 2 sharps. The first option is to use The notes in B major and D major scales over the respective chords.
Another interesting thing is D major is the ♭III chord of B major. This is a chord borrowed from B minor, the parallel minor of B major. My point is D major is the relative major of B minor. That means that you can also take an approach of alternating between B major and B minor over the D chord. They’re the same notes as playing B and D major but it gives you a different way to conceptualize it.
You can also alternate between their respective pentatonic scales, B major and D major (also the same as B minor) pentatonic.
When creating your solo try and exploit the common tones between the two scales: B C# F# (E is not ideal because it is the 4th degree of B but it can be a passing note in B). You can also move chromatically between non-common tones when the harmony changes.
Practice, experiment and see what sounds good to your ear and soon you’ll be able to improvise easily over this progression.