I have a DataSet/DataTable which I would like write changes to a MySQL database and if there are any changes in the MySQL database write those changes back to my DataSet/DataTable.
In short:
myDataSet -> MySQL database (user edits values)
myDataSet <- MySQL database (another user edited the MySQL database)
Filling the DataSet from the MySQL database is done this way:
private MySqlDataAdapter mysqlDataAdapter;
private DataSet dsTable;
private string _tablename;
public void ReadTable(string tablename)
{
_tablename = tablename;
String query = "select * from " + tablename;
if (dsTable == null)
dsTable = new DataSet();
mysqlDataAdapter = new MySqlDataAdapter(query, (MySqlConnection)connection);
MySqlCommandBuilder commandBuilder = new MySqlCommandBuilder(mysqlDataAdapter);
mysqlDataAdapter.Fill(dsTable, tablename);
}
Updating values in the table there is Row_Changed EventHandler set for the table:
private void Row_Changed(object sender, DataRowChangeEventArgs e)
{... ChangeIntegerValueInReflectedTable(...); }
where I call:
public void ChangeIntegerValueInReflectedTable(int rowIndex, string columnName, int newValue)
{
dsBom.Tables[0].Rows[rowIndex][columnName] = newValue;
}
To update the MySQL Database I use:
public void UpdateTable()
{
mysqlDataAdapter.Update(dsTable, _tablename);
}
This works. Now I would like to update changes in the remote MySQL database in my local table. There is no command for this as far as I know and mysqlDataAdapter.Update doesn't make updates back to my table only local table -> MySQL database.
So I wrote an functions which fetches the Table every 30 seconds and compares it. Then I write the changes into my dsTable. That works but next time I call UpdateTable() I get the error:
Concurrency violation: the UpdateCommand affected 0 of the expected 1 records. System.Exception {System.Data.DBConcurrencyException}
I don't know why. I change the cells in dsTable too when I update single values from the user interface.
When I call mysqlDataAdapter.Fill(dsTable, tablename) my datagridview is refreshed in a endless loop.
I'm stuck right now. Is there a good approach?