corrected the query error handling.
corrected the query error handling and better error msg.