Search code examples
.netsqlexceptionraiseerror

How to get T-SQL Raiserror custom parameters from SQLException?


I have a database table trigger that can RAISERROR at multiple point of the code. I know that we can add some custom arguments along with the RAISERROR calls (I think that those parameters are mainly for setting a custom error message).

All of this is fine.

On the .Net part of the code (C# for me), when I catch the related error, everything is almost fine, I can get it's custom message, state, severity...

... but I'm still struggling to get back my custom parameters from this C# SqlException object and I failed to find information after a long research.

My questions are :

  1. Is it possible to recover those parameters directly from the SqlException? (without having to parse the generated error message)
  2. If possible, how to get them ? Where are they hidding inside the exception object?

Thank you.


Solution

  • Finally, I found a solution.

    I'm now forgetting to pass custom parameters through the T-Sql Raiserror and I directly get the entity on error and all its values thanks to the parent exception of type DbUpdateException (in my case) which contains an Entries collection with my full entity on error.

    I just need to cast that generic type into my real entity model and I'm good to go.

    Thanks.