In I Chronicles 19, Nahash the king of Ammon has died, and Nahash’s 
son Hanun takes his father’s place as king.  David sends messengers to 
comfort Hanun because Nahash had been kind to David, probably when David
 was on the run from King Saul.  Hanun’s advisers, however, are 
suspicious, and they think that David is sending those messengers to spy
 out the land for war.  Consequently, Hanun humiliates David’s servants 
by shaving half their bodies, from the head to the buttocks.  Realizing 
that he has probably offended David by doing this, Hanun prepares for 
war, and he pays people in other countries to come and assist him.  In 
II Samuel, it was during this war that David committed adultery with 
Bathsheba, the wife of Uriah (II Samuel 11).
The Orthodox Jewish Artscroll commentary refers to Jewish 
commentators who struggled with a question.  In Deuteronomy 23:6, God 
prohibits the Israelites from seeking the peace and prosperity of the 
Ammonites and Moabites.  The rationale is stated in Deuteronomy 23:4: 
“Because they met you not with bread and with water in the way, when ye 
came forth out of Egypt; and because they hired against thee Balaam the 
son of Beor of Pethor of Mesopotamia, to curse thee” (KJV).  But here 
David was, seeking the peace and prosperity of the king of Ammon.  Was 
David violating God’s law in doing so?
The Artscroll lists solutions that commentators have proposed.  The 
first one is that David was not offering kindness to the king of Ammon 
but was simply repaying the favor that the king’s father Nahash had done
 for David.  David was not offering kindness gratis, which is what is 
prohibited, but rather is repaying a kindness.  The problem that the 
Artscroll has with this solution is that David was not repaying Nahash 
for Nahash’s kindness, for Nahash was dead; rather, David was extending 
kindness to Nashash’s son, and that did not count as a repayment, for 
Nahash’s son was not the one who had been kind to David.  Nahash was.
The second solution is that the prohibition on seeking the peace and 
prosperity of the Ammonites applies to specific situations.  The 
Israelites when making war on a city are not to offer the Ammonites 
peace, and the Israelites are not to allow any Ammonite to dwell in 
Israel as a ger toshav—-a Gentile resident alien in Israel who 
observes some of God’s commands but not the entire Torah.  These are 
forbidden, according to this solution, but extending a personal kindness
 to an Ammonite (as David did) is allowed.
The third solution is that David was aware of the prohibition in 
Deuteronomy 23:6, and he did not want to repay Nahash’s kindness while 
Nahash was still alive because that would look like he was trying to 
establish a treaty with Nahash and the Ammonites, which would presumably
 go against Deuteronomy 23:6.  Consequently, David waited for Nahash to 
die and then sought to repay Nahash’s kindness by sending his messengers
 to comfort Hanun, Nahash’s son.  That would not look like David trying 
to establish a treaty with the Ammonites, but rather it would look like 
David seeking to comfort a son who is mourning for his father.
The Artscroll itself goes another route.  It says that David in I 
Chronicles 19 felt secure, since he had defeated several nations, and he
 felt like extending goodwill to a nation that had historically been an 
enemy to Israel.  David was either trying to be overrighteous—-more 
righteous than the standard that God set forth in the Torah—-or David 
was technically compliant with the Torah yet was violating its spirit.  
According to the Artscroll, David should have realized that reaching out
 to Ammon was undesirable and would backfire, that God knew what God was
 talking about when God forbade Israel to deal with Ammon.  It did 
backfire, for the Ammonites humiliated David’s servants and geared up 
for battle.  Moreover, according to the Artscroll, the command in 
Deuteronomy 23:6 may have played a role in the Ammonite advisers’ 
suspicion of David.  They knew that the Torah of Israel forbade the 
Israelites to seek the peace and prosperity of the Ammonites, and so 
they were suspicion when David was extending kindness to their king.  
They thought David was really trying to undermine their country.
On the one hand, this discussion seems to me to stereotype an entire 
people-group as no-good, and I have issues with that, for I would prefer
 to judge people according to the content of their character rather than
 saying they’re no-good because they belong to a certain people-group.  
On the other hand, I see here wisdom that I can apply to my own life, in
 that I should not be quick to trust everyone.  I know that the New 
Testament talks about loving everybody, even enemies, and there is a 
place for that.  But Jesus did tell his disciples to be wise as 
serpents, and helpless as doves (Matthew 10:16).