سؤال

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