2

I have constraints over currency field that negative value is not acceptable. Constraints is all working fine and I am getting error when constraints violated as per below.

Unable to update row. Reason: The UPDATE statement conflicted with the CHECK constraint "CK_Product_StandardCost". The conflict occurred in database "AdventureWorksLT", table "SalesLT.Product", column 'StandardCost'. The statement has been terminated.

I am getting this error in catch block because I am updating incorrect data in table.

enter image description here

What I want is to get Column name on particular this constraint fires. Here it would be : StandardCost. Is there any way that I can find column name in my C# code?

Community
  • 1
  • 1
Ubiquitous Developers
  • 3,637
  • 6
  • 33
  • 78

1 Answers1

2

A simple Substring with IndexOf on the exception message:

var message = ex.Message;
var column = message.Substring(message.IndexOf("column '") + 8, message.IndexOf("'", message.IndexOf("column '") + 9) - message.IndexOf("column '") - 8);    
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121