The covenant between God and David is detailed in chapter 17. God promises here to make a great name for David and to subdue all his enemies (1 Chron 17: 8-10), and He promises a son who would build the Lord a house and reign on his throne forever (1 Chron 17: 11-14). The fulfillment of the first promise is demonstrated in chapters 18-20 where David and his military leaders defeat several peoples and kings. The second promise is partially fulfilled in Solomon but will ultimately be fulfilled in Jesus, son of David.
Chapter 21 speaks of a faithless act of David. This may seem at first as a harmless act that is common to governments around the world, even today. However, Israel was ultimately meant to be a theocracy where the king did the will of the Lord. The census itself was not wrong but the motivation behind it, which was likely to number the military’s strength. David failed to remember that it is not the size of the army that determines the victor but the Lord. For example, Gideon led an army of just 300 men to defeat over 120,000 Midianites (Judges 8:10-12). This was only possible because the Lord was on their side.
Something we can learn from this is that seemingly harmless actions can become sinful if we do them for the wrong reasons. For example, totally depraved unbelievers can and often do wonderful and helpful philanthropic deeds, but since they are not doing it for the glory of God, their works cannot possibly please the Lord. God judges our hearts which are desperately wicked by nature (Jer 17:9). We need to be changed by the Lord’s work of salvation before we can do anything that pleases him, so repent and trust in Him to be saved.