Insert tableAdapter method not working?

I am using ADO.NET in my C # project. In my form, I added the SourceBinding element from my toolkit in VS2010. I established a connection to the table of my dataset. It automatically creates a DataAdapter for mine.

I want to insert a record, so I call the Insert () method on the DataAdapter. But when I look at the data of my database, it has no new records ...

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

Or do I need to insert it manually using SqlCommand ???

+4
source share
2 answers

Table adapters are designed to be used with a dataset to help you retrieve data in and out of the database using this dataset.

The idea is that you can use Dataset.NewYourTableNameRow() to create a new row for your data set, then fill in its fields and then call DataSet.AddYourTableNameRow(row) to put it in the data set.

Now you can orderTableAdapter.update(DataSet) pass this new row to the database.

To delete or update a row, you must first select it in your dataset, modify the object, and then call .update (ds) in the corresponding table adapter to send it back.

+2
source

It was also difficult for me to understand this.

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

It worked for me.

So the steps are:

  • Create your binding source, table, and dataset using visual studio.
  • TableAdapter.Fill(..) // this should be automatically added vs.
  • TableAdapter.Insert(..)
  • DataSet.AcceptChanges()
  • TableAdapter.Update(..)
+4
source

Source: https://habr.com/ru/post/1333616/


All Articles