문제

I try to catch/get the Type of the OleDbException to generate the correct error message.

At this moment I'm sure I catch whatever OleDbException, but I want to catch only duplicate value violations to generate the error message.

This is what I do for the moment.

ex As OleDb.OleDbException
MessageBox.Show(ex.Errors.Item(1).ToString(), "Error Message", MessageBoxButtons.OK)

The output is like:

"Violation of UNIQUE KEY constraint 'UC_MyTable'. Cannot insert duplicate key in object 'dbo.MyTable'. The duplicate key value is (PJO, Peter Johanson)."

도움이 되었습니까?

해결책

Take a look at the .InnerException property of the exception.

See the MSDN Documentation about it.


You can also use the .ErrorCode property or just use the hash code generated by the message, which will be different for each error message:

ex.Message.GetHashCode
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top