6

I'm using ADO.NET in my C# project. In my form I added a SourceBinding element from my toolbox in VS2010. I set the connection to the table of my dataset. It creates a DataAdapter automaticly for my.

I want to insert a record, so I call the Insert() method of the DataAdapter. But when I view my database data, it doesn't have any new records...

orderID = this.orderTableAdapter.Insert("", "", 
                (int)OrderStatus.IN_CONSTRUCTION, DateTime.Now);

Or do I need to insert it manually with the SqlCommand???

Kendrick
  • 3,747
  • 1
  • 23
  • 41
Stijn Leenknegt
  • 1,317
  • 4
  • 12
  • 22

2 Answers2

4

The table adapters are designed to be used with a dataset, to help you get data in and out of the DB using this dataset.

Idea is that you can use the Dataset.NewYourTableNameRow() to create a new row for your dataset, then populate its fields and then call DataSet.AddYourTableNameRow(row) to put it in the dataset.

Now you can orderTableAdapter.update(DataSet) to transmit that new row into the database.

To delete or update a row, you would select it first into your dataset, perform a change to the object, then call the .update(ds) on the appropriate table adapter to send it back down.

Spence
  • 28,526
  • 15
  • 68
  • 103
  • Yeah but this update() doesn't persist to the database... When I retrieve data of the database, it's empty. – Stijn Leenknegt Dec 29 '10 at 15:58
  • Is your connection string correct. I've had trouble in the past where the connection string was still pointing the database to the test database instead of the live one. – Spence Dec 29 '10 at 23:06
4

I too was having difficulty figuring this out.

You need to call DataSet.AcceptChanges() after the TableAdapter.Insert(...)

That worked for me.

So the steps are:

  1. Create your bindingsource, tableadapter and dataset using visual studio.
  2. TableAdapter.Fill(..) // this should automatically be added by vs.
  3. TableAdapter.Insert(..)
  4. DataSet.AcceptChanges()
  5. TableAdapter.Update(..)
Brad Solomon
  • 38,521
  • 31
  • 149
  • 235
user890332
  • 1,315
  • 15
  • 15