My fix for issue #966
Moved code to where it belongs.
Somehow it got left in the old place, so the bug was still occurring.
Added test for add_row() failures.
Merge pull request #1294 from durga2112/issue-966

closes #966

this is a manual merge instead of using the github merge button because of a strange glitch on github