DataSet not Updating Database
-
I have a dataset that is populated from an access database and displayed in a datagrid just fine. Then I added a button to the form for updating the database. Here is the code for the button. private void bntStUp_Click(object sender, System.EventArgs e) { int iRetVal = 0; try { oDS.AcceptChanges(); iRetVal = myAdapter.Update(oDS, "tblSteps"); MessageBox.Show("The update was successful. Rows Updated: " + iRetVal.ToString()); } catch (Exception ex) { // Display information about update errors. MessageBox.Show("Update Failed: " + ex.Message); } } It works fine the dataset and datagrid are updated, but the database is not updated. Is there something that I am missing? Huh?
-
I have a dataset that is populated from an access database and displayed in a datagrid just fine. Then I added a button to the form for updating the database. Here is the code for the button. private void bntStUp_Click(object sender, System.EventArgs e) { int iRetVal = 0; try { oDS.AcceptChanges(); iRetVal = myAdapter.Update(oDS, "tblSteps"); MessageBox.Show("The update was successful. Rows Updated: " + iRetVal.ToString()); } catch (Exception ex) { // Display information about update errors. MessageBox.Show("Update Failed: " + ex.Message); } } It works fine the dataset and datagrid are updated, but the database is not updated. Is there something that I am missing? Huh?
Because you're calling
AcceptChanges
. This removes all the change information and theDataAdapter
will find none, thus it will not update the database. Just callDataAdapter.Update
, which will accept changes when the database has been updated. Read the documentation on theAcceptChanges
method (defined on both theDataSet
andDataTable
classes) in the .NET Framework SDK for more information.Microsoft MVP, Visual C# My Articles
-
Because you're calling
AcceptChanges
. This removes all the change information and theDataAdapter
will find none, thus it will not update the database. Just callDataAdapter.Update
, which will accept changes when the database has been updated. Read the documentation on theAcceptChanges
method (defined on both theDataSet
andDataTable
classes) in the .NET Framework SDK for more information.Microsoft MVP, Visual C# My Articles
-
I take the AcceptChanges line of code and I get this error. "Update requires a valid UpdateCommand when passed DataRow collection with modified rows." Now what? Huh?
It should be obvious: you need to assign a command (like a
SqlCommand
,OleDbCommand
, or whatever is appropriate) to theDataAdapter.UpdateCommand
property, using whicheverDataAdapter
is appropriate. How do you expect theDataAdapter
to update the database if it doesn't know how to update the database? If you use a simple SELECT statement, then you can also use a command builder like theSqlCommandBuilder
to build the INSERT, UPDATE, and DELETE statements for aDataAdapter
. See the .NET Framework SDK documentation for more details.Microsoft MVP, Visual C# My Articles
-
It should be obvious: you need to assign a command (like a
SqlCommand
,OleDbCommand
, or whatever is appropriate) to theDataAdapter.UpdateCommand
property, using whicheverDataAdapter
is appropriate. How do you expect theDataAdapter
to update the database if it doesn't know how to update the database? If you use a simple SELECT statement, then you can also use a command builder like theSqlCommandBuilder
to build the INSERT, UPDATE, and DELETE statements for aDataAdapter
. See the .NET Framework SDK documentation for more details.Microsoft MVP, Visual C# My Articles