0

I have the following code in my WPF project:

string query2 = "SELECT * FROM Anamnese_VMBO";
            string connectionstring = <Removed string for privacy >
            IDbConnection con1 = new OleDbConnection(connectionstring);
            con1.Open();
            OleDbCommand dbCommand = new OleDbCommand(query2, (OleDbConnection)con1);
            OleDbDataAdapter dataAdapter = new OleDbDataAdapter(dbCommand);
            OleDbCommandBuilder cb = new OleDbCommandBuilder(dataAdapter);
            cb.GetDeleteCommand(true);
            cb.GetInsertCommand(true);
            cb.GetUpdateCommand(true);
           

            dataAdapter.Fill(this.dt1);
            dt1.Merge(dt);
            dt1.GetChanges();
            dataAdapter.Update(dt1);
            dt1.AcceptChanges();        
            Grid.ItemsSource = dt1.DefaultView;
            con1.Close();

As you can see in the code, I am merging 2 data tables together and then I am performing the update command builder. When I run the code, I can see that my 2 data tables have the exact values i need but when I check my database, nothing updated.

Some things I tested:

  • Regular commandtext works when I try to insert static data. I ran a test where I manually gave the OleDBCommand an insert statement, and it worked.
  • the column names do not have special characters or are in any way invalid.
  • All tables in my databases have primary keys.
  • The table i merge with has the exact same column names.
  • I have to use the command builder because there are too many columns to manually write.
James VH
  • 69
  • 1
  • 7
  • 1
    Your OleDbCommandBuilder seems to do nothing since you don't actually use the returned values. What is it good for? What is dt and dt1? You are calling the "GetChanges" method, but again don't use it's return value. Maybe you should produce a [mcve] first? – nvoigt Mar 19 '21 at 11:46
  • This code is purely used to sort information. There are several school databases and they all have the same testname. The dt datatable is a record pulled from a global export database and then i merge this datatable with the schooldatabase it belongs in – James VH Mar 19 '21 at 11:49
  • And when i run this code, it shows exactly what needs to be done in the gridview, dt is merged with dt1 and i see the result i need in the gridview, problem is it just doesn't change in the database itself – James VH Mar 19 '21 at 11:52
  • Maybe it would help to *accept* the changes to the table *before* updating the database? That's just a guess though, I have not worked with that technology too much. – nvoigt Mar 19 '21 at 12:36
  • i tried that and it still does the same. However i just found out that if i change a value in the table it updates. It just doesn't insert the new datatable. Any ideas? – James VH Mar 19 '21 at 13:00

0 Answers0