Fix for exception on export (#1968)

This fixes an exception that would happen if three conditions were met:
  1) No database was selected or it was empty
  2) A custom export filename was set
  3) You tried to export the result of a custom query