"And it came to pass that night, that the word of the LORD came unto Natan, saying: Go and say unto My servant, unto David: Thus saith the LORD: Shalt thou build Me a house for Me to dwell in?" (2 Samuel 7:4-5). Rav said: He gave him deep counsel that night. Rabbi Chanina bar Pappa and Rabbi Simon [interpreted this]. Rabbi Chanina bar Pappa said: The Holy One, blessed be He, said to Natan the prophet: Natan, this man to whom I am sending you is quick. Hurry, before he hires workers; go and say to him, "You shall not build Me a house," so that he will not have a grievance against Me. And Rabbi Simon said: The Holy One, blessed be He, said to Natan the prophet: Natan, this man to whom I am sending you is one who makes vows. Hurry, before he vows; go and say to him, "You shall not build Me a house," so that he will not be put to shame before the Sanhedrin.
Why God Warned David in the Night Before He Built the Temple
Midrash Shmuel 26:1
ויהי בלילה ההוא ויהי דבר ה' אל נתן לאמר לך ואמרת אל עבדי [אל] דוד כה אמר ה' האתה תבנה לי בית לשבת (שמואל ב' ז' ד' ה'), רב אמר עצה עמוקה נתן לו באותו הלילה, רבי חנינא בר פפא ורבי סימון, ר' חנינא בר פפא אמר לו הקדוש ברוך הוא לנתן הנביא נתן האדם הזה שאני משלחך אצלו ממהר הוא קודם עד שלא ישכור פועלים לך ואמור לו לא אתה תבנה לי בית, שלא יהיה (להם) [לו] עלי תרעומת, ורבי סימון אמר אמר לו הקדוש ברוך הוא לנתן הנביא נתן, האדם הזה שאני משלחך אצלו נודרן הוא, קודם עד שלא ידור לך ואמור לו לא אתה תבנה לי בית, כדי שלא יהא לו בושה אצל הסנהדרין.