I've an update for this error. If I do the following it is working fine. I'm using it in this form in my actual code so that's not an issue, but the form I mentioned above should also work.
Any help will be useful. I don't want to look like a fool if my code doesn't work because because of this simple bug.