As Dave has said, you always run an executable - the difference I think you are meeting is "Development" vs "Production" code - the former executes within the Visual Studio Debugger, and the latter directly from an EXE file.
Which means that it's likely that it's your DB connection that is wrong - and we can't see that code, it's hidden behind your "cls.conn" which we have no access to.
But a good guess would be that you have the wrong connection string, or that a previous operation failed and left the connection open so the attempt to open it again failed.
Start by thinking about why you add a
try ... catch
block: it's pretty much useless if all you ever do is re-throw the exception and discard possibly useful info - a
finally
block to ensure the connection is closed and maybe some logging in the
catch
block so you can conduct a post-mortem examination of the error causes would probably help as well.
But you have bigger problems: Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.
When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;
Which SQL sees as three separate commands:
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
DROP TABLE MyTable;
A perfectly valid "delete the table" command
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.
So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?