Lets say I have a check constraint that is not respected when adding a value to a database column or lets says I have a trigger that uses RAISEERROR
with a custom message.
In my WebMethod, I use SqlException
to catch any error while updating the database. Hence I should be able to retrieve the automatic SQL Server error message if the check constraint is not respected or my custom message if the trigger raises the error message right ?
How do I do that ?
Below is what I have tried :
[WebMethod]
public string AddData(entity e)
{
using(var context = new DB_ENTITY_DbContext())
{
try
{
context.entity.Add(e);
context.SaveChanges();
return "success";
}
catch(SqlException ex)
{
return "My custom message " + ex.Message;
}
}
}
Javascript code
function AddData()
{
$.ajax({
// Some other settings
error: function (xhr, status, error){
alert(JSON.parse(xhr.responseText).Message);
alert(error);
}
});
}
Here the error does not return
"My Custom message .....(and some auto generated message)"....
It returns "Internal server error" ..
Any idea of what I should do ?