Best way of debugging code/queries

The best way to debug server errors is as already explained above:

This will return the exact server errors which will point you to what and where is wrong.