How do you override a TableAdapter method on a Table in a DataSet?
-
07-07-2019 - |
Question
I currently have a single DataSet declared which contains 3 tables. For sake of this example we will call them User, Question and Answer.
On each of these I have a TableAdapter with the various methods required, ie. GetData(), Update(), Delete() etc.
On the Answer Table I would like to override the Update Method from the TableAdapter to add some Parameters which are not part of the table, but I need to pass due to the ObjectDataSource requirement.
How do I go about overriding the Update() method on the Answer TableAdapter?
In trying to keep the project simple, I don't want to create a separate DAL layer.
Solution
Any designer-generated TableAdapter
class its appropiate members marked as virtual - this includes the Update
and Fill
method among others. Hemce, the solution is simply to inherit from the designer-generated TableAdapter
class and override the Update
method, adding your custom code there.
You also have the option of overloading the Update
method, if you want to change the method signature (parameter count/types). You could do that either on a derived class, or more conveniently in my view, using an extension method:
public static void Update(this MyTableAdapter tableAdapter, ... other params ...)
{
// do stuff here
tableAdapter.Update(...);
}