Hi group! I hope all is well!

As part of my connection string and database insert/update, I have an exception command in it to display an error message. Can this also be read or made into a true/false answer?

I would like to have my program run very short command if the insert/update should fail. I'm thinking the best way to do this may be asking if the "Catch ex As Exception" is "True Then" run my short command. Can something like this be done? Or is there a better way to do this?

In advance, thanks for the help!

Don

Dani AI

Generated

Short answer: put the clearing routine on the success path, not in Finally. As already said, execution jumps to the Catch as soon as an error occurs, and Finally always runs whether the insert succeeds or not — so placing your clear call in Finally will run even when the insert failed.

A simple, reliable pattern (no reliance on a boolean "exception is true") is to detect success explicitly. Capture the integer returned by ExecuteNonQuery (rows affected) or set a local success flag only after the database call completes successfully. Only call ClearFieldsAndVariables when rows affected > 0 (or when your success flag is true). That keeps the UI/variables untouched on failure and still guarantees connection cleanup in Finally or via disposal.

Additional practical tips:

  • Use Using blocks (or ensure disposal) for connection/command so resources are closed even if an exception is thrown.
  • Catch database-specific exceptions (for example SqlException) rather than swallowing every Exception; log the error and show a friendly message.
  • Parameterize queries to avoid injection and ensure correct types.
  • If multiple DB operations must all succeed, use a transaction and commit only on success.
  • For flaky connection strings, test the connection separately and handle transient errors (retry or surface a clear error) rather than clearing local state.

To tie back: ’s placement advice is correct; , either move ClearFieldsAndVariables into the successful code path or guard it with a rows-affected/success check so it runs only when the insert actually completed.

Recommended Answers

All 5 Replies

I don't follow. That's what the Catch portion is for. The Catch part is executed if the Catch As Exception is true.

Rev. Jim, let me try to be more specific:

I've created a 'Private Sub' that clears out the variable and textboxes one after the data is moved to the database. However, in the event that the command string fails (which is what it is doing right now), I want to put a "If/Then" statement in that will stop that sub-routine from running..... or better put, if it works, run the routine. If not, don't run it.

In thinking out loud about this, would this work correctly if I wrote the routine this way:

cmd.ExecuteNonQuery()
            Catch ex As Exception
                MessageBox.Show("Error while inserting record on table..." & ex.Message, "Insert Records")
            Finally
                con.Close()
                Call ClearFieldsAndVariables()  'This to run if the data is moved to the database
            End Try

Hopefully this explains it better.

Thoughts?

Thanks for you interest. I appreciate the help. Watch for another post I'm about to make about a connections string. The one I'm using doesn't always work. I'm not sure why. So I'll be looking for a "failsafe" command.

Don

Then what you want is

Try
    whatever command you run to do the insert
    Call ClearFieldsAndVariables()
Catch ex As Exception
    MessageBox.Show("Error while inserting record on table..." & ex.Message, "Insert Records")
Finally
    con.Close()
End Try

What happens is if the insert fails the Catch block is immediately executed. If the insert succeeds then the statements following that (in the Try block) get executed. The Finally portion gets executed whether an error occurs or not.

Rev. Jim,

If I understand you correctly, you're saying that, if the insert fails, nothing will be executed.... even the "Call ClearFieldsAndVariables" as the program will immediately jump to the "Catch ex As Exception".

Am I understanding you correctly?

As always, thanks!

Don

That is correct. When an error occurs in any line in the Try block, execution immediately transfers to the Catch block.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.