Joshua Hornby
Joshua Hornby

Reputation: 383

Changes not being saved back to database

I am trying to connect to my NorthwindDataSet, when the user clicks the button I want the data to be saved back to the database. When I run the code below I get no errors but the data is not saved. I think I am connecting to the DataSet correctly and am not sure why this isn't saving back.

NorthwindDataSetTableAdapters.CustomersTableAdapter north = new NorthwindDataSetTableAdapters.CustomersTableAdapter();
NorthwindDataSet.CustomersDataTable northtable = north.GetData();

NorthwindDataSet northwindDataSet1 = new NorthwindDataSet();
NorthwindDataSet.CustomersRow newCustomersRow =
northwindDataSet1.Customers.NewCustomersRow();

newCustomersRow.CustomerID = "5";
newCustomersRow.CompanyName = "Alfreds Futterkiste";

northwindDataSet1.Customers.Rows.Add(newCustomersRow);

northwindDataSet1.Customers.AcceptChanges();

Upvotes: 0

Views: 173

Answers (1)

user170442
user170442

Reputation:

You need to commit changes not only by AcceptChanges method but use Update method on table adapter.

In your case it will look like this:

north.update(northwindDataSet1.Customers);
northwindDataSet1.Customers.AcceptChanges();

Accept changes do not commit data to database. Update does.

Upvotes: 2

Related Questions