2 Samuel 12:16
David therefore pleaded with God for the child, and David fasted and went in and lay all night on the ground.
Possibly the lowest point in David’s life is recorded in the events surrounding this prayer—given that his sin of adultery against Bathsheba and his sin of murder against Uriah the Hittite had been revealed. Moreover, God told him that his greatest sin was against Him; consequently, He declared to him that because he had despised Him in the sins that he committed, He would raise up evil against him from his own household. He told him that because he did evil against Him secretly, He would do evil to him before all Israel. (In the rest of David’s life sin and violence in his family abounded openly.)
Well, despite the chastisement he had coming to him, David admitted his sin, and God graciously forgave him and gave him a clean heart. Then, in the next few days, judgment indeed began, and was especially extreme. God told him through Nathan the prophet that his new born son, born by Bathsheba, must die so that God’s holy reputation among the nations would be restored (v. 14).
Thus it happened that the child became very sick; and David prayed and fasted for seven days and nights, requesting that God would spare his son. But on the seventh day the child died. And David “arose from the ground, washed and anointed himself, and changed his clothes; and he went into the house of the Lord and worshiped. Then he went to his own house; and when he requested, they set food before him and he ate” (v. 20). Thus David, by refusing to grieve after his child died, demonstrated to God and to his household that he was not bitter against God, but accepted his punishment as just.
Here we see the lesson that sometimes prayer and fasting is necessary but with no apparent answer. But I think there was certainly an answer; for during that time of prayer and grieving God was softening David’s heart and bring him to an absolute right relationship with Him.