Concurrency violation: the UpdateCommand affected 0 of the expected 1 records.
-
Hi there, When I use the following code below I get and error called , "concurrency violation" and the database is not updated. I am able to connect and retrieve data. but I cannot update the data from the datatable of dataset. I am new to datagridview and datasets. I am using Mysql 5.0 and Mysql connector .NET 5.1.2 I read many articles on updating the dataset and almost tried everything , but It does not work, I can update to Mysql database using update commands. But I want to use Dataset or dataadapter update to make it more simpler. public partial class Form1 : Form { DataSet ds = new DataSet(); MySqlDataAdapter da; DataTable dt = new DataTable(); string tabname; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { CreateMySqlCommand(); } // Connecting to db and filling the datagridview with data. public void CreateMySqlCommand() { MySqlConnection myConnection = new MySqlConnection("Persist Security Info=False;userid=root;password=XXXX;database=test;server=terrain"); myConnection.Open(); MySqlTransaction myTrans = myConnection.BeginTransaction(); string mySelectQuery = "SELECT * FROM patient_master"; MySqlCommand myCommand = new MySqlCommand(mySelectQuery, myConnection,myTrans); da = new MySqlDataAdapter(mySelectQuery ,myConnection) ; MySqlCommandBuilder cmdBldr = new MySqlCommandBuilder(da); da.Fill(ds); dt = ds.Tables[0]; DataRow row; row = dt.Rows[0]; int lirow = dt.Rows.Count; string mrno = row["MRNO"].ToString(); string patname = row["GIVEN_NAME"].ToString(); dataGridView1.DataSource = ds; dataGridView1.DataMember = ds.Tables[0].ToString(); tabname = dt.TableName; int y = 0; foreach (DataGridViewRow ro in dataGridView1.Rows) { if (y % 2 == 0) ro.DefaultCellStyle.BackColor = Color.Red; y++; } } // code on the button to update the datadapter private void button1_Click(object sender, EventArgs e) { da.Update(ds, tabname); } } }
-
Hi there, When I use the following code below I get and error called , "concurrency violation" and the database is not updated. I am able to connect and retrieve data. but I cannot update the data from the datatable of dataset. I am new to datagridview and datasets. I am using Mysql 5.0 and Mysql connector .NET 5.1.2 I read many articles on updating the dataset and almost tried everything , but It does not work, I can update to Mysql database using update commands. But I want to use Dataset or dataadapter update to make it more simpler. public partial class Form1 : Form { DataSet ds = new DataSet(); MySqlDataAdapter da; DataTable dt = new DataTable(); string tabname; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { CreateMySqlCommand(); } // Connecting to db and filling the datagridview with data. public void CreateMySqlCommand() { MySqlConnection myConnection = new MySqlConnection("Persist Security Info=False;userid=root;password=XXXX;database=test;server=terrain"); myConnection.Open(); MySqlTransaction myTrans = myConnection.BeginTransaction(); string mySelectQuery = "SELECT * FROM patient_master"; MySqlCommand myCommand = new MySqlCommand(mySelectQuery, myConnection,myTrans); da = new MySqlDataAdapter(mySelectQuery ,myConnection) ; MySqlCommandBuilder cmdBldr = new MySqlCommandBuilder(da); da.Fill(ds); dt = ds.Tables[0]; DataRow row; row = dt.Rows[0]; int lirow = dt.Rows.Count; string mrno = row["MRNO"].ToString(); string patname = row["GIVEN_NAME"].ToString(); dataGridView1.DataSource = ds; dataGridView1.DataMember = ds.Tables[0].ToString(); tabname = dt.TableName; int y = 0; foreach (DataGridViewRow ro in dataGridView1.Rows) { if (y % 2 == 0) ro.DefaultCellStyle.BackColor = Color.Red; y++; } } // code on the button to update the datadapter private void button1_Click(object sender, EventArgs e) { da.Update(ds, tabname); } } }