Firstly, I would check whether the SQL statement you are sending is syntactically and result-wise correct.
I would test this with an external programme.
As I don't know which operating system you are using, I can't recommend one.
And the I'd follow af0815's advice to step-debug your code to see where exactly a problem exists.
It would also be interesting to see how exactly you create the SQL statement in the code. Even better would be a small test project against an SQL database so that you can see the problem in real life