views:

87

answers:

1

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();
+1  A: 

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.

Aviad P.
Thanks for the tip. I figured out what was really causing the problem. I made a dumb oversight and forgot to populate a required field. Thanks, though.
basic
Wait a second, you're using AcceptChanges after all and it works? If that's the case I must have been doing things wrong all along...
Aviad P.
I removed AcceptChanges and it worked. But yes, I just found out you are right, having AcceptChanges does not allow the data to be written. I actually tried not having AcceptChanges in my code but it still didn't work and that was because I wasn't filling a required field.
basic