To err is human. The Prophet (peace and blessings be upon him) said: “should you not commit sins, Allah would do away with you and would bring forth other people to commit sins and seek forgiveness so that He would grant them forgiveness.” (Sahih Muslim)
What a Muslim is required to do is to be on his guard and whenever he makes a mistake or commits a sin, he should hasten to seek forgiveness and implore humbly to Allah to accept his repentance and strengthen his will so as not to commit that sin once again.
It is true that sins may be a cause for not answering one’s du’a, but there may be some other reasons for not immediately fulfilling what a person asks for. Allah the Almighty is all-knowing and all-wise.
Sometimes the wisdom is not known to us, so what we have to do is to feel content with His will and His decree.
Shedding more light on this, Sheikh Ahmad Kutty, said:
While it is true to say that Almighty Allah hears and answers prayers, it is not true that He answers each and every petition that we make exactly in the same way that we wish them to happen.
If this were to happen, then the world would be a chaotic place to live; for humans may pray for all kinds of things, which may appear reasonable to them when judged from their own purely subjective points of views, at a specific time or place. We can see the absurdity of this, when we reflect on our own past. Each of us may have had the experience of thinking and praying for something in the past, and we were distressed when it did not happen as we had prayed for. However, after years passed, we feel that it was not good for us after all. This is because now we have in our possession the facts that convince us of the futility of our earlier wishes and dreams. Therefore, while Almighty Allah answers prayers, we cannot assume that all of our prayers will materialize as we imagined or according to our own desires.
Almighty Allah answers prayers, but He alone knows what is good for us, for He has all the facts governing our lives—past, present and future, and thus, He alone knows what is good for us in an ultimate sense. Therefore, while assuring us that our prayers are heard by Almighty Allah, the Prophet (peace and blessings be upon him) taught us the following: “No Muslim shall pray to Allah for anything— other than that entails sins or severing ties of kinship—without Allah granting him one of three things by virtue of his/her prayer: He may grant what they pray for (if it is good for them); or keep them as a treasure to grant them in the next word, or by virtue of their prayer avert some calamities that are decreed to happen to them!” On hearing this, the Prophet’s companions, remarked, “In that case we will pray more and more! Then the Prophet (peace and blessings be upon him) replied, “Allah’s generosity is even more boundless!” (ِAl-Bayhaqi- Shu’ab al-Imam)
Therefore, prayers are always beneficial for us and we should pray unceasingly, regardless of whether a particular wish has been granted or not, for Almighty Allah alone knows what is in our best interests, and He always does what is best for His servants. We are therefore best advised to seek peace by submitting to His will; we may do so by reading and meditating upon the following prayer of our beloved Prophet (peace and blessings be upon him):
Allahumma ij`al lee nafsan mutma’innatan tu’minu bi liqa’ika, wa taqna`u bi `ata’ika wa tarda bi qada’ika wa takhshaka haqqa khashyatika (O Allah, grant me a soul that firmly believes in meeting You, and is content with the provision You have apportioned me, and is satisfied with Your decree, and fears You as You ought to be feared.)