How can I catch specific exception using c# ?
In my database there is unique index on some columns.
when user inserts duplicate record this exception has been throw :
Cannot insert duplicate key row in object 'dbo.BillIdentity' with unique index 'IX_BillIdentity'. The statement has been terminated.
How can I catch this exception?
Currently I am checking using this code :
catch (Exception ex) { if (ex.Message.Contains("Cannot insert duplicate key row in object 'dbo._BillIdentity' with unique index 'IX__BillIdentity")) { string ScriptKey = "$(function() {ShowMessage('Error');});"; ScriptManager.RegisterStartupScript(Page, GetType(), "script", ScriptKey, true); } } I think its bad smell code.
Is there any better way?