Advertisement
joy007

GetDuplicateEntryErrorMessage

Aug 29th, 2023
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. private string? GetDuplicateEntryErrorMessage(DbUpdateException ex)
  2. {
  3. var sqlException = ex.InnerException as SqlException;
  4.  
  5. if (sqlException != null)
  6. {
  7. // Extract the duplicated values from the error message
  8. string errorMessage = sqlException.Message;
  9. int startIndex = errorMessage.IndexOf("(") + 1;
  10. int endIndex = errorMessage.IndexOf(")");
  11. string duplicateValues = errorMessage.Substring(startIndex, endIndex - startIndex);
  12.  
  13. string errorDescription = endIndex > startIndex ? "Duplicate Values" : "Duplicate Value";
  14.  
  15. return $"Duplicate Value: {duplicateValues}";
  16. }
  17.  
  18. return null; // No duplicate entry error
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement