"it not work" is not a very helpful description of the problem.
What doesn't it do that you expected, or what does it do that you didn't expect?
What line of code does it start going wrong?
Have you tried adding some debug statements to display where it's up to and what the variable values are? This could help you determine what is wrong.

In fact if you're using Visual Studio, have you tried stepping through it in the debugger? Watching it execute line by line with the ability to watch variable contents is a spectacularly efficient way to find bugs.