One of my favorite aspects of programming is the amount that you can learn through failure. Whether it be through the failure of tests when working within a test-driven development framework or simply debugging syntax errors in your code, there is always a lesson to be learned.
The same is true of most other aspects of life, for example, when you have a poor job performance review at work, or fail an exam while studying at university, you learn from the experience and become a better person because of it. The biggest difference when coding is the instantaneous nature of the feedback, which allows you to learn that much more quickly.
Of course, this can also be incredibly frustrating, because sometimes when you fail in programming it can take hours or days to find out where you went wrong. You put in an immense amount of effort, and see very little for your struggles. But I think this is where the most learning can take place.
Eventually you will figure out how to solve your problem, and will have that in your toolbox for the future. But, in the meantime, you have been digging through your code, poring over Stackoverflow, diving into documentation, and asking your friends questions. The amount of focused thought and research that you do when trying to overcome a coding roadblock ends up giving you an even greater understanding of the languages and frameworks you are working with than would be the case if you had simply sailed through the project.
So even though it is sometimes difficult to stay motivated when you hit a roadblock, it helps to remember that that roadblock is going to make you a better programmer in the future.