2

I want to insert data into MS Access using a DataSet and TableAdapters, but unfortunately it doesn't update the table in MS Access when I run this code:

DataSet2TableAdapters.Tabelle1TableAdapter tabelle1TableAdapter = new DataSet2TableAdapters.Tabelle1TableAdapter();

DataSet2 dataSet2 = new DataSet2();
dataSet2.Tabelle1.Insert("hallo", 1);
tabelle1TableAdapter.Update(dataSet2.Tabelle1);

This is just a test file in my actual MS Access file the table has a lot of columns so I really like to use the "Insert" method or something similiar because it's easy to write.

Some suggestions how I can accomplish this?

  • 1
    `Fill` will fill the `DataSet`-table(in memory), it won't update the database. You have to use `tabelle1TableAdapter.Update(dataSet2.Tabelle1)` – Tim Schmelter Dec 04 '17 at 13:06
  • ok just use update or call it after the fill? –  Dec 04 '17 at 13:07
  • 1
    First fill the DataSet or DataTable, then modify something, then call `Update`. Omit the `AcceptChanges`. But you don't need to fill the whole table, you can also add a single row programmatically and then call `Update`. That will insert the new record – Tim Schmelter Dec 04 '17 at 13:08
  • while debugging i see that my table is empty after calling the fill method so there must be the error –  Dec 04 '17 at 13:19
  • that is a completely different issue. You have to configure the select command by providing the correct sql query(which you haven't shown). But as mentioned, that is not necessary if you just want to add rows. – Tim Schmelter Dec 04 '17 at 13:21
  • Make sure your database file is not getting overwritten in your compile/execute cycle. Everything you added in one execution may get lost next time you compile your project, with the database file being reset to its initial state. – jsanalytics Dec 07 '17 at 04:18
  • There's a few troubleshooting steps you can take in order to establish if this is actually your problem and eventually solve it: 1) in the solution explorer window, right-click your database and select **Properties**. Change **Copy to Output Directory** to **Do not copy**. 2) After compiling your application, run it a couple of times **outside** VS and the debugging environment and verify if the problem goes away. 3) Modify your existing code to reload your data, from the database, **right after** an insertion or deletion has been successfully executed and verify if the problem goes away. – jsanalytics Dec 07 '17 at 14:30
  • 1
    Thank you for the hint! Yes there is a copy in the Output Directory where the database gets filled with data. If you put this info into an answer i will accept it –  Dec 07 '17 at 14:51

2 Answers2

2

One common pitfall in this situation is that your database file may get overwritten in your compile/execute cycle. Everything you added in one execution may get lost next time you compile your project, with the database file being reset to its initial state. VS stores the original DB file in your project root folder. Your application uses its own copy stored in the bin folder (or somewhere else).

There's a few troubleshooting steps you can take in order to establish if this is actually your problem and eventually solve it: 1) in the solution explorer window, right-click your database and select Properties. Change Copy to Output Directory to Do not copy. 2) After compiling your application, run it a couple of times outside VS and the debugging environment and verify if the problem goes away. 3) Modify your existing code to reload your data, from the database, right after an insertion or deletion has been successfully executed and verify if the problem goes away.

Or, if you know exactly where the file is located, simply verify that the records were actually properly stored.

jsanalytics
  • 13,058
  • 4
  • 22
  • 43
1

Don't fill your table instead just use the insert function from adapters.

Something like.

DataSet2TableAdapters.Tabelle1TableAdapter tabelle1TableAdapter = new DataSet2TableAdapters.Tabelle1TableAdapter();

Tabelle1TableAdapter.Insert(valueForID,ValueForWert,ValueForNummer);
Tabelle1TableAdapter.Update(dataSet2.mytable);

You only use fill to read the data from the database

myadapter.fill(mydataset.mytable); // will read the data

Read More

Krish
  • 5,917
  • 2
  • 14
  • 35
  • this was my first approach but it didn`t work either –  Dec 06 '17 at 22:18
  • 1
    Then you need to dig into why it didn't work. There is a large quantity of material on how to use ADO.Net on the web. Also David Sceppa's ADO.Net 2.0 book - still relevant. – peterG Dec 07 '17 at 09:57