Click to See Complete Forum and Search --> : Update from a view


dzonka
November 8th, 2008, 06:06 PM
ADO.NET offers a great functionality which makes it very easy to implement databinding (two directional - read and write). Let`s assume that we have a DataSet with some simple DataTable tied with TableAdapter.

Here is what we have to do:
1) Create DataTable with the standard GetData method from the Adapter
2) Assign this datatable as binding source for our datagridview on UI
3) When user makes some changes in the datagridview, it is enough to call 'Update' method on the Adapter to transfer all changes into database.

This is very easy and fast to implement. However it does not work when our DataTable contains data from more than 1 physical tables from database (any joins in select clause).

Why is it impossible? The update method on tableAdapter does not generate at all. How to solve this?

Thank you very much for any help!

TheCPUWizard
November 8th, 2008, 06:14 PM
The "write" methods (Insert,Update,Delete) can only be generated if the table is loaded via a means that:

1) is a single source
2) has a unique key

If EITHER of these are not true, it is impossible to automatically update the DB.

You MUST write the commands manually.

Note that for any "real" program this should ALWAYS be done, and all of the commands (including the "Load" command) should be calles to Stored Procedures in order to minimize both attack surface and potential for bugs.
[ie the application accound should NOT have any access to tables/views in the DB]

vuyiswam
November 11th, 2008, 04:47 AM
The Adapter Wizard is Limited if you want to Update from a Join. Its because the Adapter does not know what record to update. Now its only limited to a Single table. That means you have to write the Data layer on your own now. Well i must say i started doing my Database work using Wizards and Microsoft has done a very good Job on them . But when you have to Update from a Join its a Problem. I have Written an Article that Solve the Problem, well not Solving the Problem , but Writting your code without using any Wizard.

check here

http://www.codeproject.com/KB/cs/N-Tier22.aspx


Hope this Helps