473,387 Members | 1,578 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,387 software developers and data experts.

Rollback context changes in Linq Winforms application.

I'm creating DbLinq MDI WinForms application.
It allows edit customers list in DataGridView. Every row change is submitted
separately.
As Nicholas recommends, it is reasonable to create database Context object
Db in form constructor and keep it alive in form existence.

I use following command to delete row when user selects delete row command:

Db.Customers.Remove( customer );
try {
Db.SubmitChanges();
} catch (Exception ex) {
// Error =we MUST roll back modifications
UIManager.Error(ex, "Cannot delete a row");
}

In case of delete error (e.q foreign key violation) I need to rollback the
delete operation in Db.

Any idea how to do this ?
Currently deleted customer remains in Db. Invalid delete keeps submitted in
every SubmitChanges() making any future SubmitChanges() impossible.

Andrus.
Jan 19 '08 #1
2 3152
I'm creating DbLinq

Yet again : you are probably one of the few people on this list using
this third-party LINQ dialect, so I'm not sure how many people *here*
will be able to give you a good answer. Try asking the author?

Anyway, in LINQ-to-SQL, although there is Attach, there is no obvious
Detach mechanism - but the change-conflicts are presented via the data-
context. Another option would be to look at the change-set to
construct a new data-context *just* of the changes, and try submitting
that - and just don't keep adding it if you know it has failed once.

But in reality - at this point your data-context is unreliable. I'd be
very tempted to simply ditch it...

Marc
Jan 19 '08 #2
Andrus,

In addition to what Marc said, I should add that I never said that you
should create the DataContext in the form constructor. I believe that what
I said is to evaluate the data operations that you are performing, and then
create a DataContext there.

What you will want to do is create a TransactionScope instance (this
assumes that your data provider supports detecting an ambient transaction
and registering a connection with it). You can use that when you begin your
delete operation, and if it succeeds, call Complete, and it will commit all
the work. If Complete is never called, then entire operation is rolled
back.

If it fails, you are going to want to get rid of the DataContext, as you
will have an inconsistency, I am sure.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com


"Andrus" <ko********@hot.eewrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
I'm creating DbLinq MDI WinForms application.
It allows edit customers list in DataGridView. Every row change is
submitted separately.
As Nicholas recommends, it is reasonable to create database Context object
Db in form constructor and keep it alive in form existence.

I use following command to delete row when user selects delete row
command:

Db.Customers.Remove( customer );
try {
Db.SubmitChanges();
} catch (Exception ex) {
// Error =we MUST roll back modifications
UIManager.Error(ex, "Cannot delete a row");
}

In case of delete error (e.q foreign key violation) I need to rollback the
delete operation in Db.

Any idea how to do this ?
Currently deleted customer remains in Db. Invalid delete keeps submitted
in every SubmitChanges() making any future SubmitChanges() impossible.

Andrus.

Jan 20 '08 #3

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

Similar topics

6
by: Arsen V. | last post by:
Hello, I have a localization class that I want to use from either Web or Windows Forms apps. Currently it stores some information in the HttpRuntime.Cache object. I want to be able to determine...
2
by: Sparky | last post by:
Hi I have an vb.net winforms application that needs to launch a thread, and within that thread change the security context of the current user to a new one using impersonation. For clarity, the...
2
by: Ian Boyd | last post by:
We're encountering a situation where we're encountering a deadlock, and someone's been made the deadlock victim. But after that, DB2 refuses to run any SQL, and instead we get the error message: ...
0
by: yogeeswar | last post by:
Hi All I wrote SP with number of delete commands in an atomic block.And there is a possibility of deleting records from parent table before child table so I wrote a handler to handle the...
0
by: kinglalit | last post by:
Hi, My DBA wants to turn off the explicit rollback segments currently being used and replace them by UNDO segment. I have explicit mention of these rollback segments in my C++ and Pro*C codes....
1
by: Martijn B | last post by:
Hello there, I was woundering if there is a solution to the following problem i have. Is it possible with VS2003 to undo local changes made from a certain (defined) startpoint to a (defined)...
4
by: Andrus | last post by:
For winforms application with multiple related forms it is reasonable to create Linq database context object in start of application. Context object is released only when application exits. So...
1
by: VictorLeo | last post by:
I have an application that this throws with VS 2008 and LINQ everything works well, the problem is when I try to create an installer for this project. Adding a project to install add the main...
3
by: Vivien Parlat | last post by:
Hello, I am currently using VB.Net 2008 express. I use linq to perform queries on a database, and I'm using the following link's source to convert those queries into DataTables i can then bind...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.