Which entity failed during collection save

Posts   
 
    
carmines
User
Posts: 15
Joined: 25-Oct-2006
# Posted on: 14-Nov-2006 22:57:15   

If you get an error when saving an entity collection using .SaveMulti can you find out which entity in the collection caused the error (for example a duplicate pk error)?

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 15-Nov-2006 03:23:02   

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();
                }
    }
}
carmines
User
Posts: 15
Joined: 25-Oct-2006
# Posted on: 16-Nov-2006 22:29:42   

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

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 17-Nov-2006 12:08:46   

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.

Frans Bouma | Lead developer LLBLGen Pro