Is the error occurring on this application 6 page, or on the next page that you are getting to? The query that printed out looks ok to me, have you tried running this query manually on your db to see what the result is? If you can run this query and do not get the same error then it is probably not this query that is causing the error.
It can be difficult to debug sql queries when you do not get back a meaningful error message like this. If we can find the query that is bad and print it out we should be able to quickly determine why it is not correct.