ImGreg
ImGreg

Reputation: 2993

C# TableAdapter Insert Method Missing

I am using the C# VS2010 TableAdapter Wizard for my project to interface with my db. I have generated my own Insert, Update and Delete commands for this adapter since it uses a join and can not generate its own. I'm okay with that, but unfortunately when I created the Insert command I am not able to call the "Insert()" method on the tableadapter. The other methods are working fine.

For example,

`this.joinTableAdapter = new MyTableAdapter();

this.joinTableAdapter.Insert() <-- does not exist.`

It was my understanding that once you create a valid insert command that this method should become visible on the table adapter. Am I mistaken about this? It is working for my update and delete command. Any ideas what I am doing wrong?

Any help is appreciated. Thanks everyone!

Upvotes: 1

Views: 8404

Answers (3)

Chris
Chris

Reputation: 1

Go into the dataset click on the tableadaptor and check setting for property "GenerateDBDirectMethods" - Needs to be true

Upvotes: 0

jjthebig1
jjthebig1

Reputation: 638

There is a 4th way of working with JOIN queries.

For the table adapter's main query, omit the join. Then, add manually the columns to the data table and set their SourceColumn to the exact spelling of the fields you will add. Finally, add a new query to the table adapter that includes the join and call this query to fill the datatables in your application.

If this is too abbreviated, let me know. I'll elaborate more.

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460238

TableAdapter GenerateDbDirectMethods

In addition to the InsertCommand, UpdateCommand, and DeleteCommand, TableAdapters are created with methods that can be executed directly against the database. These methods (TableAdapter.Insert, TableAdapter.Update, and TableAdapter.Delete) can be called directly to manipulate data in the database.

If you do not want to create these direct methods, set the TableAdapter's GenerateDbDirectMethods property to false (in the Properties window). Additional queries added to the TableAdapter are standalone queries — they do not generate these methods.

You'll find this option at the designer in the second last page. Create methods to send updates directly to the database

Note: This is not the same then clicking Advanced Options and click:

Generate Insert, Update, and Delete statements

When you select this option, the wizard will attempt to generate INSERT, UPDATE, and DELETE statements based on the SELECT statement defined on the Generate SQL statements page.


Edit:

The TableAdapter cannot create insert,update and delete statements automatically when more than one table is involved in the main select query. There are several approaches if you need one or more columns of related tables, the best way depends on your requirement.

  1. Add insert,update and delete queries manually to the TableAdapter. These methods can be called directly from an instance of the TableAdapter(f.e. InsertUser(param1,param2))
  2. Add insert,update and delete commands manually to the TableAdapter(designer or programmatically). These commands will be triggered implicitely from the TableAdapter when you call Update. The DataRow's RowState determines which command will be triggered(f.e. Added->InsertCommand).
  3. Select one table and join the tables via relations to other tables in the DataSet

For the third approach you could use LINQ-To-DataSet to join the related tables to get all needed columns(f.e. to create a DataSource for a GridView).

Upvotes: 1

Related Questions