user200014
user200014

Reputation:

Inserting Row into an Access 2003 DB using TableAdapter

I'm new to using TableAdapters and I am unsure of what is going on. Essentially, I am trying to export data from one database to another database with a different schema using a generated dataset. Everything seems to be working fine, in terms of populating the row of the target database, when I step through the code. However, when I try to have a row added to the target database, the row does not seem to get inserted. Do you guys have any ideas? I have set the database that was added to the project set to not copy to the output directory...so the suggestions I saw on the web didn't seem to work.

        OleDbConnection oleDbConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Database\database.mdb;");

        SomeTableAdapter tableAdapter = new SomeTableAdapter();
        tableAdapter.Connection = oleDbConnection;
        tableAdapter.Connection.Open();

        SomeDataSet.SomeDataTable dataTable = tableAdapter.GetData();
        SomeDataSet.SomeDataRow dataRow = null;

        // Do some checks on the existing rows

        // Creation of new row is necessary
        if (dataRow == null)
            dataRow = dataTable.NewSomeRow();

            // Populate row fields

            dataTable.AddSomeRow(dataRow);
            dataTable.AcceptChanges();
        }
        else
        {
            // Update exiting row
        }

        tableAdapter.Update(dataTable);
        tableAdapter.Connection.Close();

Upvotes: 1

Views: 1492

Answers (1)

Aviad P.
Aviad P.

Reputation: 32639

You are shooting yourself in the foot by calling AcceptChanges(). It marks all the modifications to the dataset as 'committed' so the next Update simply ignores them.

You generally shouldn't ever call AcceptChanges directly, except in advanced scenarios.

Upvotes: 2

Related Questions