"Right" way to delete a datatable record using auto-generated ADO.Netobjects in C# 2005

D

Dathan

Hello, all.

I've created a data set using the designer in Visual Studio 2005, with
C# as the target language. I get a data set, a bunch of custom data
tables, and a bunch of table adapters.

I have a main form MainForm. It keeps a "master" data set for the
application. To simplify the interface, I spawn child forms, and
create copies of the appropriate portions of the data set in each
child form. Something like:

ChildForm1 new_form = new ChildForm1();
// copy data to the child form
new_form.DataSet.DataTable1.Load
(this.DataSet.DataTable1.CreateDataReader());
new_form.ShowDialog();
// when the user's done with the child form, merge changes back into
the master
this.DataSet.DataTable1.Load
(new_form.DataSet.DataTable1.CreateDataReader(), LoadOption.Upsert);
..
..
..
// when we're closing the application, or when "commit" button is
pressed, commit data changes to the database
this.dataTable1Adapter.Update(this.DataSet.DataTable1);

This works fine for updating existing rows and adding new rows, but
will not commit row deletions to the database. The code I'm using to
delete rows is (within ChildForm1):
((DataRowView)this.dataTable1BindingSource.Current).Delete();

where dataTable1BindingSource is created by the designer when set
DataTable1 as the DataSource for a listbox within ChildForm1.

Can someone point me in the right direction?

Thanks!
~Dathan
 
G

Gregory A. Beamer

As I see it, you are coding more for the exception than the rule. You are
pulling a huge amount of data into the master and then doling it out to the
child forms. Unless the master is going to control this data, it is a bad
pattern. And, even if it is, you are dealing with a non-optimal scenario for
..NET. There is one possible exception I can think of and that is when your
windows application is disconnected for long periods of time.

If this is a disconnected application, I would look at Dino Espositos
articles and find the one on disconnnected ADO.NET applications. I wish I
had an URL for you. If you cannot find one, post back and I will see if I
have time to find it, or write up a new blog entry on the problem.

If it is not, you are better to retrieve small bits of data as you need
them.

As far as the delete issue, since you are using table adapters, I find it
easier to craft a delete method on the table adapter and fire it when the
delete occurs (bearing in mind the difference if this is a disconnected
app). You can then refresh the DataSet and rebind it.Much of this is
automagic in Windows Forms, but I scrap a lot of the automagic to make sure
my app logic can translate easily to other UIs. Your mileage may vary.

It is late, so I hope this is making some sense.

--
Gregory A. Beamer
MVP: MCP: +I, SE, SD, DBA

Blog:
http://feeds.feedburner.com/GregoryBeamer

********************************************
| Think Outside the Box! |
********************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top