Send updates and deletions to sql all at once?

A

Adam Maltby

Hi,

I have a datagrid bound to a dataset using .SetDataBinding

Once the info is loaded into the table and the user has edited it (be that adding new rows, deleting or modifying rows) I then need to write them back to sql2000.

Two questions:

Since the datagrid is bound to the dataset, any changes in the grid should update the dataset?

If the above is true, can I just use the dataset merge to apply changes to sql?

Cheers
Adam
 
G

Guest

The DataAdapter is the bridge between the database and the DataSet. To get
data back in, the easiest method is to use the Update() method on the Adapter
responsible for the data in question. Behind the scenes, it compares the
original rows with current rows, and other fun stuff.

A simple merge on the DataTable will, at best, replace the current rows. It
will not talk back to the database "automagically" by merging all of the
variations of the rows.

I would take a look at Update(). Dino Esposito has some great articles on
concurrency with ADO.NET on the MSDN site.

---

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

***************************
Think Outside the Box!
***************************
 
A

Adam Maltby

Ok, but all the samples I have seen deal with the data (prob becasuse they are only demos) having loaded a DA, DS etc loaded the data, chanegd it and wrtten back all in the same sub.

How ouwld that work if I had a load button and save button sine the da would only be availabel to that button? (I tried putting the da etc in general decs but it throws an exception)

Cheers
Adam
 
S

Sahil Malik

Adam,

Further explaining Gregory's point, changes to the dataset, done via the datagrid (because it is bound), will not automatically reach the database.

You would need the use a SqlDataAdapter to do this job. Once you get the changed dataset, you have a multitude of options -

a) Send in the changes as XML as an updategram into the d/b. (In my opinion the mostest bestest :) )
b) Create a command and call it for the changes in the dataset. (A good option)
c) Infer the command using commandbuilder et-al (A bad option).

This code that uses a SqlDataAdapter, will be called at the click of the "save" button. Given that a dataset is disconnected, it doesn't matter if the "fill" and "update" are in the same sub or not.

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
Please reply to the newsgroups instead of email so everyone can benefit from your reply.


Ok, but all the samples I have seen deal with the data (prob becasuse they are only demos) having loaded a DA, DS etc loaded the data, chanegd it and wrtten back all in the same sub.

How ouwld that work if I had a load button and save button sine the da would only be availabel to that button? (I tried putting the da etc in general decs but it throws an exception)

Cheers
Adam
 

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