Home > Enterprise >  Ways to save changes in dataGridView
Ways to save changes in dataGridView

Time:03-24

I have a database with 8 tables, i fill my datagridview and then save changes using the same block of code for each table, but i can only save 4 of 8 tables, the other 4 tables gives me an error Concurrency violation: the UpdateCommand affected 0 of the expected 1 records whe i try to save the changes in them

This is how i fill my dataGridView:

{
string script = "SELECT * FROM hatr.rbt;";
mycon = new MySqlConnection(connect);
mycon.Open();
MySqlDataAdapter ms_data = new MySqlDataAdapter(script, connect);
SD.DataTable table = new SD.DataTable();
ms_data.Fill(table);
DataSet ds = new DataSet();
dataGridView1.DataSource = table;
mycon.Close();
ds.AcceptChanges();
}

And this is how i save the changes:

{
string script = "SELECT id, name, Model_preparation_R_Hr, Time_for_preparation_hr, Time_for_post_processing_hr, YZV_work FROM rbt;";
mycon = new MySqlConnection(connect);
mycon.Open();
MySqlDataAdapter ms_data = new MySqlDataAdapter(script, connect);
var cb = new MySqlCommandBuilder(ms_data);
cb.GetInsertCommand();
DataSet ds = new DataSet();
ms_data.Update(dataGridView1.DataSource as DataTable);
ms_data.InsertCommand = cb.GetInsertCommand();
ms_data.InsertCommand.CommandText  = "; select * from rbt where id = last_insert_id();";
ms_data.InsertCommand.UpdatedRowSource = UpdateRowSource.FirstReturnedRecord;        
}

I also know that the error occurs in the line:

ms_data.Update(dataGridView1.DataSource as DataTable);

Both blocks of code are the same for every table, but the second block which is need to save changes in dataGrid for some reason gives me an updateCommand error i mentioned before, i couldn't figure out why i get this error for some tables and why i dont get it for others. So maybe there is some other way to save changes in dataGridView? Because this is the only one i could come up with for now

CodePudding user response:

I finally have found a reason of the Concurrency violation: the UpdateCommand affected 0 of the expected 1 records error. Considering that i have created my database in MySql Workbench and i was trying to work with it through dataGridView in my App i have found out that there is a conflict between FLOAT fields in MySql Workbench and dataGridView. When you create a float number in MySql Workbench it should be written with a dot like "0.1" and it appears that in dataGridView it should be written with comma like "0,1" so the one float field but in to different situations: in mysql Workbench and dataGridView has two different ways to be written and it occurs an Error, as i see this situation, i can be wrong in some ways of course, but i just recreated all FLOAT fields as DOUBLE and now it works.

  • Related