The webassist DataBridge extension can help you create recordsets and update code instead of using hand coding. I can see your hand code has SQL Injection vulnerabilities and it is difficult for me to debug remotely. If you wanted to use a premier support ticket I could help you debug what is wrong with your hand written code.