1 Chronicles 27:23,24 (KJV)
23 But David took not the number of them from twenty years old and under: because the Lord had said he would increase Israel like to the stars of the heavens.
24 Joab the son of Zeruiah began to number, but he finished not, because there fell wrath for it against Israel; neither was the number put in the account of the chronicles of king David.
1 Chronicles 27
What We Think Is Good, God May Think Otherwise
What David had Joab do, Joab knew in the back of his mind that it was wrong and God would not be happy with it.