1. David commits adultery with Bathsheba and arranges the death of Uriah, Bathsheba’s husband.
‘Things were getting too easy for David; he had the leisure to stay at home while Joab and his men were out fighting Ammonites and Syrians (1 Chron 20:1). One evening as he was attempting to cool off, he looked doen from his palatial rooftop at his neighbour’s wife. Leisure and lust led to adultery and then to murder, which sins had eternal repercussions as well as tragic earthly results. It is one of the most shocking and serious warnings of the Old Testament that a man may be ever so good and great and still have weaknesses that can lead to deeds that entirely overshadow and defeat his better nature and reduce his eternal potential. It is often the case that a man’s greatest tests come at times of his greatest successes. It seems to be the case in most situations of sin that the offender considers himself to be above some commandment, to be an exception to a rule.’ (Andrew C Skinner and D Kelly Ogden, Verse by Verse, The Old Testament)

2 Samuel 11:2. Why Was David Walking on His Roof?
‘Many homes in the Holy Land, both then and now, had flat roofs. In the heat of the Middle East, much of the people’s time was spent walking or sitting on their roofs in the refreshing cool of evening or in the day to catch a daytime breeze. The roof of David’s palace was probably high enough that he could have looked into the inner courts of a number of homes nearby.’ (Institute Old Testament Manual)
2. David is told that he will be punished because of his sins.
2 Samuel 12:1–4. Murder and Adultery Carry Their Own Consequences
“As happens too frequently, it is only when a sinner knows that his sin is known that he begins to repent! The figure of Nathan boldly accusing the king to his face by an allegorical parallel is impressive, though not as surprising in Bible stories as it would be in accounts of other peoples where the will of God was not such a recognized factor in determining the morality of men and in specifying the results. Nathan’s allegory was skillfully drawn, and his climatic ‘Attah ha ish!’ (‘Thou art the man’) must have crashed in upon the conscience of David like the harbingers of doom’s day.
“His repentant feelings were no doubt sincere, but he could not repent enough to restore the life of his friend, Uriah, nor the virtue of his wife. Though he later hoped and prayed that his soul would not be left forever in hell (the spirit prison), yet the eternal destiny of doers of such twin sins does not look good. (See Psalms 16 and 51; then see Hebrews 6:4–6; Revelation 22:14–15; D&C 132:27; 76:31–37; 29:41 and 42:18, 79.)” (Rasmussen, Introduction to the Old Testament, 1:185.)
2 Samuel 12:7 Thou art the man
‘What a terrible word! And by it David appears to have been transfixed, and brought into the dust before the messenger of God.
Thou Art this son of death, and thou shalt restore this lamb Fourfold. It is indulging fancy too much to say David was called, in the course of a just Providence to pay this fourfold debt? to lose four sons by untimely deaths, viz., this son of Bath-sheba, on whom David had set his heart, was slain by the Lord; Amnon, murdered by his brother Absalom; Absalom, slain in the oak by Joab; and Adonijah, slain by the order of his brother Solomon, even at the altar of the Lord! The sword and calamity did not depart from his house, from the murder of wretched Amnon by his brother to the slaughter of the sons of Zedekiah, before their father’s eyes, by the king of Babylon. His daughter was dishonored by her own brother, and his wives contaminated publicly by his own son! How dreadfully, then, was David punished for his sin! Who would repeat his transgression to share in its penalty? Can his conduct ever be an inducement to, or an encouragement in, sin? Surely, No. It must ever fill the reader and the hearer with horror. Behold the goodness and severity of God! Reader, lay all these solemn things to heart.’ (Clarke’s Commentary on the Bible)

3. A repentant David seeks forgiveness.
‘David’s poignant plea for forgiveness after his sin with Bathsheba is expressed in Psalm 51. Out of the agony of his repentance comes marvelous doctrinal truth. The intent behind the offering of sacrifices is taught – to witness to god a broken heart and contrite spirit (v17), characterstics the Saviour himself exhibited as he worked out the infinite and eternal atonement.’ (Andrew C Skinner and D Kelly Ogden, Verse by Verse, The Old Testament)

Psalm 51:1 Tender mercies
‘David’s words show that even in Old Testament times, the Lord’s people understood that their hearts must be given to God, that burnt offerings alone were not enough.
The sacrifices mandated during the Mosaic dispensation pointed symbolically to the atoning sacrifice of the Messiah, who alone could reconcile sinful man with God. As Amulek taught, “Behold, this is the whole meaning of the law, every whit pointing to that great and last sacrifice; … the Son of God” Alma 34:14′ (Bruce D Porter, General Conference, October 2007)