There is also 2 Samuel 12. David had Uriah the Hittite killed, which is wrong because the Bible says, “Thou shalt not kill” (Ex 20:13). So God decided to kill David’s baby because He is just (Deut 32:4), even though David had repented and God had forgiven him (2 Sam 12:13).
2 Sam 12:15b-20a
15 The Lord struck the child that Uriah's wife bare unto David, and it was very sick.
16 David therefore besought God for the child; and David fasted, and went in, and lay all night upon the earth.
17 And the elders of his house arose, and went to him, to raise him up from the earth: but he would not, neither did he eat bread with them.
18 And it came to pass on the seventh day, that the child died. And the servants of David feared to tell him that the child was dead: for they said, Behold, while the child was yet alive, we spake unto him, and he would not hearken unto our voice: how will he then vex himself, if we tell him that the child is dead?
19 But when David saw that his servants whispered, David perceived that the child was dead: therefore David said unto his servants, Is the child dead? And they said, He is dead.
20 Then David arose from the earth, and washed, and anointed himself, and changed his apparel, and came into the house of the Lord, and worshipped.
David prayed and fasted for seven days, interceding for his unborn child. But God denied his request. God cannot go back on His word. If He makes a vow, He will fulfill it. He said He would kill David’s baby and He did.
Num 23:19
God is not a man, that he should lie; neither the son of man, that he should repent: hath he said, and shall he not do it? or hath he spoken, and shall he not make it good?
In the end, David did the right thing. After God ignored his prayers and killed his baby, he went to the tabernacle and worshipped God.
God is always worthy of praise, even if He doesn’t answer your prayer.