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?

4
  • you need to parse the exception message Commented Aug 17, 2017 at 13:45
  • @Rahul - You mean I need to fetch from exception message by some logic? Commented Aug 17, 2017 at 13:47
  • Yup, exactly .. what I mean Commented Aug 17, 2017 at 13:48
  • even thought I fetch from the string, what could be the best logic for it? I can think about substring text between last '' (single quote pair). Is it best solution? Commented Aug 17, 2017 at 13:50

1 Answer 1

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); 
Sign up to request clarification or add additional context in comments.

3 Comments

this is working. But is there anyway to get column name in exception stack? and no need to do this kind of manipulation over message?
Not that I know of.
Confirmed. The error returned from SQL Server contains that string, and there is no built-in API for extracting data from it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.