Nezreli
Nezreli

Reputation: 1288

How to recognize SqlException where user cancelled?

So, I cancelled a query using SqlCommand.Cancel. Sql client returned a SqlException with the following message:

A severe error occurred on the current command. The results, if any, should be discarded. Operation cancelled by user.

Fine by me. But, I have to gracefully deal with it since it's not a regular error which should be reported. The problem is, besides the message, there is no indicator inside the SqlException object that I've found which would identify that it was caused by a user cancelling the query.

I don't want to use the message itself, because it could be changed at some point or translated into another language. The Error Number property is 0 and Class property is 11.

Upvotes: 0

Views: 2083

Answers (1)

I-A-N
I-A-N

Reputation: 169

For SQL Server 2008 R2 and .NET 4.0 the following properties of SqlException are set:

If the SqlConnection.FireInfoMessageEventOnUserErrors property is set to true:

  • Class: 16
  • Number: 3204
  • Message: The backup or restore was aborted. RESTORE DATABASE is terminating abnormally. Operation cancelled by user.

If the SqlConnection.FireInfoMessageEventOnUserErrors property is set to false:

  • Class: 11
  • Number: 0
  • Message: Operation cancelled by user.

Upvotes: 6

Related Questions