Reputation: 3132
I am doing a small project to learn how to use DataSet
but i have a small problem. Consider following code:
foreach (DatabaseDataSet.ApplicationRow rowApplication in database.Application)
{
if (rowApplication.AID.ToString() == lblIDApplication.Text)
{
rowApplication.Date= tbApplicationDatum.Text;
rowApplication.Status = tbApplicationStatus.Text;
applicationAdapter.Update(rowApplication);
break;
}
}
I don't know why but the database doesn't get updated. The DataRow is being updated as when I call the data again I see the new value. But when I re-run my application it's back to it's old value again. Any help?
EDIT: I'm working with strongly typed DataSet
Upvotes: 1
Views: 4023
Reputation: 3132
SOLUTION: It happens that nothing was wrong with the code. I had two ConnectionString defined in App.config. I forgot to remove the first one after I removed a previous database that had errors in it. Upon removing the first ConnectionString, everything worked.
Upvotes: 1
Reputation: 3724
Its important to remember that the DataSet is a 'local copy' of the data not a 'live link' to the DB. If your DataSet is populated by a IDataAdaptor (say a TableAdaptor) for example you need to call the DataAdaptors Update method passing in the Updated dataset to sync the results back to the underlying DB.
Also I would suspect you DONT want to be doing 'new ApplicationTableAdapter()' because typically you would want to update with the TableAdaptor you populated with, at the least you would need to ensure you had the correct connection, query etc set up.
Upvotes: 2
Reputation: 38230
You need to call the Update
method of your adapter to propogate the changes
AcceptChanges
only updates the changes in memory for the row and does not migrate those to the database
MSDN AcceptChanges and RejectChanges only apply to DataRow related changes (that is, Add, Remove, Delete, and Modify). They are not applicable to schema or structural changes.
Calling AcceptChanges will not replicate these changes back to the data source if the DataSet was filled using a DataAdapter. In that situation, call Update instead See Updating Data Sources with DataAdapters for more information
Upvotes: 3