The file download code has to be on the top of the page before any other code runs. You will have to move the code from lines 281-286 above to line 27.
That isn't what is causing the error though. The error is because you have specified a varchar column as the key column in the compliance_manual_vault2 table. The download count code currently only works with numeric key columns... usually a unique key or auto-increment column.
If you have a numeric key in that table, then you should be able to update the download server behavior to use that key and it will fix the problem. If you need to get it to work with a varchar column, post back and I'll give you instructions on what code needs to be updated.