Why was it a sin for David to count the people but not for Moses?

Why in 2 Samuel 24 was it counted a sin in David to number the children of Israel, whereas Moses was commanded by God to do so? (Exodus 30:12).

Perhaps the very point our questioner notes as to Moses having been commanded to take the census of the people goes some way toward solving this difficulty. A thing may be commanded at one time, and not at another. For instance, the Feast of Tabernacles was commanded to be observed on the I 5th day of the seventh month, but to keep the feast in the 8th month, as Jeroboam did “out of his own heart,” was a flagrant offence (1 Kings 12:32).  So, spurious imitations of God’s ordinances to-day become positive acts of disobedience; for instance, baptising infants, or making of the breaking of bread an idolatrous act, the “eucharist” or the mass. Moses was commanded in Exodus 17:6 to smite the rock, but when he did this in Numbers 20:11-12, it was counted a sin unto him. In Exodus 30. 12 the Lord gives a direction to be followed when an occasion arose for numbering the people—each one was to give a half shekel as a ransom for his soul unto the Lord. It was, so to speak, a recognition before the Lord that the life numbered was in reality forfeited and needed to be ransomed. There is no mention of this being observed in David’s case, though that was a command to the people in general. In Numbers 1:2, 3, Jehovah commanded Moses to take the sum of all the congregation of the children of Israel—”From twenty years old and upward, all that are able to go forth to war in Israel.” It was clearly a matter of order and importance that the number of the people should be known at the commencement of their desert journeyings, and it is given as 603,550. Then again in chap. 26, Moses and Eleazar were commanded to number the people at the close of their wanderings. They actually shewed a decrease of nearly 2,000, surely an eloquent commentary on the effect of their disobedience. Had they taken possession of the land when this was proposed to them they would, no doubt, have shown a large increase over a like period. In the case of David’s census, we read that “the anger of the Lord was kindled against Israel and He moved David against them to say, Go, number Israel and Judah” (2 Samuel 24:1), of which census a man of the world like Joab, and also the captains of the host, saw the futility. Joab’s words are remarkable; he expresses the desire that God might add a hundred-fold to the people, that the king might see the increase, i.e. without any need of a census. In 1 Chronicles 21:1 we read, “Satan stood up against Israel,” which simply means that Jehovah in inflicting judgment on His people for their transgression, allowed Satan to move in the matter as His agent. Men may make difficulties and cavil at God’s ways, but He is Sovereign and He will be justified by all, when His ways and the secrets of men are known. In reality there was no command to David from Jehovah to number the people. It was an arbitrary act of self-will and self-glorying, resulting in the infliction of the plague on the very ones on whom God would inflict judgment. We see, then, in the “Samuel” account God in government carrying out His judgment on David and the people—He is the ultimate cause; but in the “Chronicles,” Satan is presented as the instrumental cause—He tempts David to engage in the path of self-will, and the King allows himself to be led astray and is responsible, as he admits himself (2 Samuel 24:17). David, however, knows the grace of God, as well as His government, and his prayer is: “Let me fall now into the hands of the Lord; for His mercies are great: and let me not fall into the hands of man” (v. 14); and so he is led to build an altar unto the Lord and offer burnt-offerings and peace- offerings, typical of the sacrifice of Christ.