Solved

TransactionScope rollback.

Posted on 2011-03-24
1
855 Views
Last Modified: 2012-05-11
Hi

How to rollback a TransactionScope. I tried adding new elements List in a transaction using TransactionScope and never called the complete method. Even after that the methods inside the Transaction are executed and are not rolled back. Below is the sample code.

I was expecting output to be
1
2
1

but the actual output was
1
2
2

Could anyone tell me how to get the expected output using the below code.

Thank You

static void Main(string[] args)
        {
            List<string> foo = new List<string>();
            foo.Add("a");
            Console.WriteLine("Before Transaction: "+foo.Count);
            
            try
            {
                using (TransactionScope ts = new TransactionScope(TransactionScopeOption.RequiresNew))
                {                    
                    foo.Add("b");                    
                    Console.WriteLine("During Transaction: "+foo.Count);
                }
            }
            catch 
            {
            }

            Console.WriteLine("After Transaction: " + foo.Count);            
            Console.Read();
        }

Open in new window

0
Comment
Question by:mkarthik415
1 Comment
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 35211415
TransactionScope is related to database transactions and it has no impact on collections (List<T> in your example).

More about TransactionScope here:
http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to open a link from vb.net app 3 10
Create XML 5 35
Asp.net mvc 5 5 23
Get Reply from SSIS to intranet site when finished 3 14
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now