Reference: http://www.alfawzan.af.org.sa/node/14836
Question: I am a youth, and have been afflicted with [committing] a sin, and every time I repent [to Allaah] from it, I go back to doing it; I [just] want to be free of it, [so] if I were to do it again, is it permissible for me to undertake an oath that I shall fast a day for the Sake of Allaah, or give in charity so [as a result of which] He will safeguard me from doing it again?
Response: It is forbidden to undertake an oath, so do not take an oath. However, remain resolute in repenting often, and do not despair from the Mercy of Allaah – saying ‘how many times I have repented and yet, yet, yet; do not despair from the Mercy of Allaah. Every time you sin, then repent to Allaah ‘Azza wa Jall [as He says]:
[So] do not despair from the Mercy of Allaah, [rather, continue in] repent[ing] to Allaah.