title: How to determine if an exception is of a particular type
body: <p>I have a piece of try catch code:</p>
  
  <pre><code>try 
  {
      ...
  }
  catch(Exception ex) 
  {
      ModelState.AddModelError(
          "duplicateInvoiceNumberOrganisation", "The combination of organisation and invoice number must be unique");
  }
  </code></pre>
  
  <p>For this piece of code I'm trying to insert a record into a database: The dba has set it up so that the database checks for duplicates and returns an error if there are duplicates. Currently, as you can see, I'm adding the same error to the model no matter what error occurred. I want it changed so this error is only added to the model if it was caused by the duplicate error set up by the dba.</p>
  
  <p>Below is the error I want to catch. Note it's in the inner exception. Can anyone tell me how to specifically catch this one?</p>
  
  <p><img src="https://i.stack.imgur.com/LHMa3.jpg" alt="enter image description here"></p>