The code below at the very bottom of this post is the event that gets called when I press a button. The idea of the code is to make modifications to a Sql Server database to commit changes made in a DataGridView that loads data from the SQL server. When no changes have been made to the data, and I hit the commit button that calls this code, it throws an exception So in other words, for some reason the code Code: DataTable changes = table.GetChanges(DataRowState.Modified | DataRowState.Added); returns a row EVEN though no cell was modified, so that Code: dataAdapter.Update(changes); throws the exception. How on earth does one prevent this? nitro Code: private void Commitbutton_Click(object sender, EventArgs e) { try { DataTable changes = table.GetChanges(DataRowState.Modified | DataRowState.Added); if (null != changes) { Console.WriteLine(changes.TableName + " has Changes: " + changes.Rows[0].RowState.ToString()); if (DataRowState.Unchanged == changes.Rows[0].RowState) return; table.EndInit(); BindSource.EndEdit(); dataAdapter.Update(changes); } } catch (SqlException sqlex) { Console.WriteLine(sqlex); } catch (Exception ex) { Console.WriteLine(ex); } }
rosy, Thanks for the response. I was not accepting changes into either the changes table, or table. If I do that, it solves the SqlException problem. However, then no commits occur at all! The Sql Server database does not reflect the data in the DGV when I AcceptChanges() on either table. Which table are you saying I should call AcceptChanges on, and where in the code? nitro
I though table.AcceptChanges() would work ... table.AcceptChanges(); try { DataTable changes = table.GetChanges (DataRowState.Modified | DataRowState.Added); maybe an explicit commit is necessary somewhere in there.
I changed the code thinking more deeply about your suggestion to the code below. It works. Thank you. I owe you a beer. nitro Code: private void Commitbutton_Click(object sender, EventArgs e) { try { dataAdapter.Update(table); BindSource.EndEdit(); table.EndInit(); table.AcceptChanges(); } catch (SqlException sqlex) { Console.WriteLine(sqlex); } catch (Exception ex) { Console.WriteLine(ex); } }