views:

39

answers:

1

Hello there, I'm trying to catch the exception throwen when I insert a already existing user with the given username into my database. As the title says then I'm using EF. The only exception thats throwen when I try to insert the user into to db is a "UpdateException" - How can I extract this exception to identify whether its a duplicate exception or something else?

+4  A: 
catch (UpdateException ex)
{
    SqlException innerException = ex.InnerException as SqlException;
    if (innerException != null && innerException.Number == ??????)
    {
        // handle exception here..
    }
    else
    {
        throw;
    }
}

Put the correct number at ?????? that corresponds to unique constraint violation (I don't know it from the top of my head).

Darin Dimitrov
amazing, you answered that in the same time i wrote the comment. +1 :)
RPM1984
Perfect :) - However.. I just tried to catch the ErrorCode and it says that the error code for a duplicate entry is -2146232060 which seems a bit odd to me?
ebb
My bad.. used errorCode instead. Thanks :)
ebb