TextBox change is not saved to DataTable
Posted
by
SeaDrive
on Stack Overflow
See other posts from Stack Overflow
or by SeaDrive
Published on 2012-10-26T16:33:01Z
Indexed on
2012/10/26
17:01 UTC
Read the original article
Hit count: 175
I'm having trouble with a simple table edit in a Winform application. I must have missed a step.
I have a DataSet containing a DataTable connected to a database with a SqlDataAdapter. There is a SqlCommandBuilder on the SqlDataAdapter. On the form, there are TextBoxes which are bound to the DataTable. The binding was done in the Designer and it machine-produced statements like this:
this.tbLast.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.belkData, "belk_mem.last", true));
When I fill the row in the DataTable, the values from the database appear in the textboxes, but when I change contents of the TextBox, the changes are apparently not being going to the DataTable. When I try to save change both of the following return null:
DataTable dtChanges = dtMem.GetChanges();
DataSet dsChanges = belkData.GetChanges();
What did I forget?
Edit - response to mrlucmorin:
The save is under a button. Code is:
BindingContext[belkData, "belk_mem"].EndCurrentEdit();
try
{
DataSet dsChanges = belkData.GetChanges();
if (dsChanges != null)
{
int nRows = sdaMem.Update(dsChanges);
MessageBox.Show("Row(s) Updated: " + nRows.ToString());
belkData.AcceptChanges();
}
else { MessageBox.Show("Nothing to save.", "No changes"); }
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
I've tried putting in these statements without any change in behavior:
dtMem.AcceptChanges();
belkData.AcceptChanges();
© Stack Overflow or respective owner