Is there anything wrong with my code?

You have changed your code but not posted the new version and it is not possible to help you any further until you do.