473,320 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Check if the database must be saved when you close a C# form (with bound controls)

Hi,
I'm writing a C# program that interfaces with a SQL Server 2005
database (I use Visual Studio 2005 Professional Edition as compiler).
I have defined the classical ADO.NET SqlDataAdapter, DataSet and
DataTable objects so that, once the database has been loaded with the
SqlDataAdapter Fill method, you work with the data off-line.
I would like to put a control in the Form OnClosing event handler
method: if there are some changes pending, there should be a MessageBox
that asks the user if he wants to save the changes; otherwise (no
changes pending), the Form closes without warnings.
The only way I have thought to do that is the following:

protected override void OnClosing(System.ComponentModel.CancelEventArgs
e)
{
Base.OnClosing(e);

DataRow[] adrChangedRows = dtTable.Select("", "",
DataViewRowState.Added |

DataViewRowState.ModifiedCurrent | DataViewRowState.Deleted);

if (drChangedRows.Length 0)
{
DialogResult dlgChange = MessageBox.Show("The database has
changed; do you want to

save the changes?", "Warning",

MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);

if (dlgChange == DialogResult.Yes)
daTable.Update(drChangedRows);
else if (dlgChange == DialogResult.No)
dtTable.RejectChanges(); // probably unuseful, but for clearness
else // if (dlgChange == DialogResult.Cancel)
e.Cancel = true;
}
}

Do you think there is a faster (and more elegant) way to do that?

Thank you very much

Oct 25 '06 #1
2 1804
Hi,

There's no better place to prompt the user and cancel the operation than in
the Closing event, IMO.

But you might want to use one of the DataSet.HasChanges method overloads
instead of dtTable.Select(...).

Also, you may want to look into control validation events and properties to
prevent attempting to save incomplete or bad data.

--
Dave Sexton

"polocar" <po*****@tin.itwrote in message
news:11*********************@k70g2000cwa.googlegro ups.com...
Hi,
I'm writing a C# program that interfaces with a SQL Server 2005
database (I use Visual Studio 2005 Professional Edition as compiler).
I have defined the classical ADO.NET SqlDataAdapter, DataSet and
DataTable objects so that, once the database has been loaded with the
SqlDataAdapter Fill method, you work with the data off-line.
I would like to put a control in the Form OnClosing event handler
method: if there are some changes pending, there should be a MessageBox
that asks the user if he wants to save the changes; otherwise (no
changes pending), the Form closes without warnings.
The only way I have thought to do that is the following:

protected override void OnClosing(System.ComponentModel.CancelEventArgs
e)
{
Base.OnClosing(e);

DataRow[] adrChangedRows = dtTable.Select("", "",
DataViewRowState.Added |

DataViewRowState.ModifiedCurrent | DataViewRowState.Deleted);

if (drChangedRows.Length 0)
{
DialogResult dlgChange = MessageBox.Show("The database has
changed; do you want to

save the changes?", "Warning",

MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);

if (dlgChange == DialogResult.Yes)
daTable.Update(drChangedRows);
else if (dlgChange == DialogResult.No)
dtTable.RejectChanges(); // probably unuseful, but for clearness
else // if (dlgChange == DialogResult.Cancel)
e.Cancel = true;
}
}

Do you think there is a faster (and more elegant) way to do that?

Thank you very much

Oct 25 '06 #2
Ok Dave, thank you

Oct 30 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Chris Windsor | last post by:
I hope the following describe what I'm trying to do: I have created a tool to be used by product analysts when studying different cell phone designs. Part of the tool is a set of 11 forms on a...
2
by: aaj | last post by:
Hi all I have a continuous bound form and on each record is a tick box. The user ticks the boxes and these boxes define the batch. for future operations before they leave the page I count...
0
by: Patrick | last post by:
I'm working on a contact management application, and need a hand with one aspect... Here's what I want to create: ------------------------------------ A form split into two parts. There is a...
7
by: Neo Geshel | last post by:
Greetings. I have a serious problem. I have multiple sets of tables, several of which are chained more than two tables deep. That is, I have a parent, a child, and a great-grandchild table. ...
4
by: Jonathan Upright | last post by:
Greetings to anyone who can help: I'm using WebMatrix to make ASP.NET pages, and I chose the "Editable DataGrid" at the project selector screen. As you may know, it defaults to the Microsoft...
7
by: vsiat | last post by:
I am trying to create a treeview out of a database table with the typical structure ID, NAME, PARENTID, TYPE, EXTRA_INFO, where is linked to the . What I want to achieve is create a tree made...
8
by: colmkav | last post by:
Hi, could someone tell me how I can check whether a database is open by name eg something like db("mydbname")
1
by: =?Utf-8?B?SHVzYW0=?= | last post by:
Hi EveryBody: I made web site using asp.net 2.0 Vb.Net. The project depends on database in the local machine. The web site has • Create User Wizard and • Login form When the user is...
0
by: bloukopkoggelmander | last post by:
Hi All wonderfull brains! Right I have two questions after my last very successfull thread. I have tried looking these up on the net, but no luck. Scenario 1 is : I have a bound form with bound...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.