7

I have the following problem qith my local SQLite database. I'm trying to update the data stored via a synchronisation-process with an online SQL-Server database. While there is no problem with the synchonization I often encounter errors when trying to update the local values. E.g. there can be an violation with existing Primary-Key-Constraints on each table. On the other hand there could also be a violation of the Unique-Key on a concrete table.

My question is how to figure out which type of error occured.

I catch the database update or insert like

catch(UpdateException ue)
{
    HandleUpdateException(ue);
}

The handle method is as follows:

private void HandleUpdateException(UpdateException e)
{
    // get inner exception as SQLiteException
    var innerException = e.InnerException as SQLiteException;

    if(innerException != null)
    {
        switch(innerException.ErrorCode)
        {
            case SQLiteError.Constraint:
                // handle constraint-error here
                break;
            default:
                // log error
                break;
        }
    }
}

How do I now which error occured? It's important to know because the handling of PK-violation is completely other than a UK-violation.

I could try to make

if(e.Message.Contains("unique")) // ...

or

if(e.Message.Contains("foreign")) // ...

but I don't like to check with "magic" strings for the kind of error.

So any help would be appreciated.

KingKerosin
  • 3,639
  • 4
  • 38
  • 77
  • 1
    The information you want will likely only be contained in the message. You could purposely cause an exception, then in a debugger inspect the exception to see if there are better values that you could check. Also, I would recommend doing String.IndexOf("unique", StringComparison.OrdinalIgnoreCase) >= 0 due ot case issues. – Stefan H Jul 19 '12 at 16:46
  • 1
    I already checked the other values contained in the UpdateException and also the SQLiteException. But both (PK and UK violations) are handled under Constraint-Error which is not wrong at all, but not enough information. – KingKerosin Jul 19 '12 at 16:48
  • 1
    That is what I figured. Sorry, but it looks like you will have to parse the message property for the specific exception type that you want to handle. Good luck. – Stefan H Jul 19 '12 at 17:01
  • 1
    Ok. Not the best way but obviously the only thing that will work. Thanks anyway for your help – KingKerosin Jul 19 '12 at 17:03

0 Answers0