Question

I have an app which worked fine with Sql Server. I have a DevExpress grid which shows just a record in carousel mode (not that this matters, I hope).

Now, I have changed the code to be database-agnostic and I'm testing MySql. When the user modified the record and accepted the changes, I was getting the following error:

Concurrency violation: the UpdateCommand affected 0 of the expected 1 records

After some research, I've come to the conclussion that the problem lies in DATETIME fields. I am using "Allow Zero Datetime=False; Convert Zero Datetime=True;" in my MySql connection string so I can convert default DATETIME values to .Net DateTime objects. The autogenerated UpdateCommand includes every field in the where clause, and I guess the comparison fails when the MySql DATETIMEs are set to the default value, as removing DATETIME fields the problem went away.

I have a Primary Key column, and the user isn't allowed to modify it, so what's the right way to issue a custom UpdateCommand so that there's only one field in the WHERE clause?

My current code for accepting changes:

Dim builder As DbCommandBuilder = m_Conn.CreateCommandBuilder(m_Adapter)
m_Adapter.Update(m_DataTable)

CreateCommandBuilder is an extension method on IDbConnection to create the correct an object with a correct implementatin of the DbCommandBuilder interface.

Was it helpful?

Solution

Your DBCommandBuilder should have a ConflictOption Property that needs to be set. Presumably you want to set it to ConflictOption.OverwriteChanges.

I'm not sure if it works when you initialize the Adapter commands via the CommandBuilder Constructor but a

var builder = new MySqlCommandBuilder();
builder.ConflictOption = ConflictOption.OverwriteChanges;
builder.DataAdapter = m_Adapter;

should do.

OTHER TIPS

Instead of using "Allow Zero Datetime=False; Convert Zero Datetime=True;" in your connection string (which FYI I'm not familiar with), I'd recommend using DateTime.Parse(value). You'll probably want to write a function so that you can easily handle nulls as well.

private DateTime getDateTimeField(string dbValue)
{
    if (dbValue == null)
    {
        return new DateTime();
    }
    else {
        return DateTime.Parse(dbValue);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top