mans
mans

Reputation: 18188

Problem adding a row into a typed dataset

I have an access database and I created a typed dataset for it using Visual Studio (.xsd) and I want to add a new record into it but I have problem as it doesn’t add the data into database and there is no error. My code is as follow (summarized):

MyProjectDataSet.PAddressDataTable t=(MyProjectDataSet.PAddressDataTable)MyDataSet.Tables["PAddress"];
            var r = t.NewPAddressRow();
            r.PID = 44;  // Person ID which this address belong
            r.Address1 = "Line1";
            r.Address2 = "Line2";

            t.Rows.Add(r);
            r.AcceptChanges();
            t.AcceptChanges();

        PAddressTableAdapter.Update(r);

I can see that after I add the new row to table, it appears on table with ID=-1. But when I update the rows I cannot see it on access database and there is no error.

What is wrong with this code? I don't want to use Insert, as the number of fields that I have is very high and Insert doesn't accept Row data.

Upvotes: 1

Views: 682

Answers (2)

Carter Medlin
Carter Medlin

Reputation: 12495

Do not "AcceptChanges" before you perform the "Update". "Update" relies on the change information in the "DataTable" to determine what operation to perform. When the "Update" is complete, any rows committed to the database will have their changes automatically accepted after their data operations are complete.

Upvotes: 0

user863767
user863767

Reputation: 11

Remove

r.AcceptChanges();

t.AcceptChanges();

lines and try again.

Upvotes: 1

Related Questions