473,800 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(Syste m.ComponentMode l.CancelEventAr gs
e)
{
Base.OnClosing( e);

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

DataViewRowStat e.ModifiedCurre nt | DataViewRowStat e.Deleted);

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

save the changes?", "Warning",

MessageBoxButto ns.YesNoCancel, MessageBoxIcon. Warning);

if (dlgChange == DialogResult.Ye s)
daTable.Update( drChangedRows);
else if (dlgChange == DialogResult.No )
dtTable.RejectC hanges(); // probably unuseful, but for clearness
else // if (dlgChange == DialogResult.Ca ncel)
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 1827
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.HasChan ges 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.it wrote in message
news:11******** *************@k 70g2000cwa.goog legroups.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(Syste m.ComponentMode l.CancelEventAr gs
e)
{
Base.OnClosing( e);

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

DataViewRowStat e.ModifiedCurre nt | DataViewRowStat e.Deleted);

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

save the changes?", "Warning",

MessageBoxButto ns.YesNoCancel, MessageBoxIcon. Warning);

if (dlgChange == DialogResult.Ye s)
daTable.Update( drChangedRows);
else if (dlgChange == DialogResult.No )
dtTable.RejectC hanges(); // probably unuseful, but for clearness
else // if (dlgChange == DialogResult.Ca ncel)
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
1594
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 tab structure with fields for various features a phone might have; there are almost 100 of these features to choose from. Rather than a simple check box, I used a combo box with the a value list of "Y, N, N/A, and ?"; I even simplified entry by...
2
2365
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 the number of ticks using (ADO) rst.Open "SELECT COUNT(update_po_ref) AS update_qty FROM dbo.tbl_forecasted
0
3646
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 datagrid on the left side that lists names and perhaps a couple of other key fields. The user can click on a record in the datagrid, which should automatically pull up details on that record in the various text boxes and other controls on the right...
7
2017
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. Currently, I am allowing the parent to be edited by itself. In order to get to a child table, a user needs to select a specific parent table ID. I have set this up using panels, and putting a drop-down list in the first panel, with its contents...
4
2151
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 SQL database "pubs". I've followed the instructions in the comments and also changed everything pertaining to SQL over to OLEDB. (i.e. Changed SqlDbType. to OleDbType.) I also changed the datafield names and variable names accordingly. The page...
7
2839
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 of custom, extended nodes, which include all the extra information contained in the table and not just typical TreeNode objects. To do that, I first created a structure with all the extra
8
2184
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
2596
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 created the username and password saved in the database. And the code is as follows: Protected Sub CreateUserWizard1_CreatedUser(ByVal sender As Object, ByVal e
0
1250
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 controls. For one of the controls I look up the values from a table, but the values I then sleect from this combo box, is concatednated, ie. Lastname,Firstname,Employee number. Now what I want is that once the user has selected a concatenated name...
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10505
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10253
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10035
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9090
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7580
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6813
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5471
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.