carmines wrote:
bclubb wrote:
Here's something where I check for ORMQueryExecutionException. This exception is thrown when an issue such as a duplicate primary key is thrown. The exception contains the DbParameters for the entity that failed. You can use these parameters to identify the entity that failed. I am using it in this code to populate a textbox with the offending Primary Key.
try
{
names.SaveMulti(false);
}
catch (ORMQueryExecutionException ex)
{
foreach(System.Data.Common.DbParameter parameter in ex.Parameters)
{
if (parameter.ParameterName == "@KeyId")
{
textBox1.Text = parameter.Value.ToString();
}
}
}
Thanks bclubb - your comments have helped but ...
I appreciate this is just a simple example to illustrate the parameters property and uses my original posting of a duplicate PK error.
However, what it was a different error and you want to find out what the error was?
I can see the innerexception contains the SqlException and that has an error number so great.
But can the ORMQueryExecutionException only ever have SqlException as the inner exception?
What if it has another type of inner exception - do you need to test what type the inner exception is?
Any insights would be very helpful
It can have other types of exceptions as well. In general you should do something like: (this is a rough layout, to illustrate the point)
try
{
// do some save logic
}
catch(ORMQueryExceutionException ex)
{
// something went wrong, check inner exception
try
{
throw ex.InnerException;
}
catch(SqlException sx)
{
// handle
}
catch(Exception e)
{
// something else, bubble upwards
throw;
}
}
you can also check with 'is' the type of the inner exception and act accordingly. It's essential you specify 'throw;' and not 'throw e;' in the last guard clause, to preserve the stacktrace.