Guessing because you haven't included what the error message is or on which line.
Place a breakpoint on the lines where you're creating the query and check the values of the variables that you're concatenating to make sure the values are what you are expecting.