0

I am not talking about transactions at database level. I woukld like to implement transactions at object level. Here is my code:

WorkQueue wq = new WorkQueue();
wq.OWNER_ID = user.EmployeeId;
wq.OWNER_NM = user.FullName;
wq.WQ_DETAILS = txtDetails.Text;
wq.SOURCE_INFO = txtSource.Text;
wq.PRIORITY_ID = Convert.ToInt32(ddlRequestType.SelectedItem.Value);
wq.PRO_ID = Convert.ToInt32(ViewState["pro_id"]);
wq.GROUP_ID = Convert.ToInt32(ViewState["cat_id"]);
wq.Save();

WQAttachment attachment = new WQAttachment();
attachment.ATTACH_SIZE = FileUpload1.PostedFile.ContentLength.ToString();
attachment.ATTACH_FILE = FileUpload1.FileBytes;
attachment.WQ_ID = wq.WQ_ID.Value;
attachment.ATTACH_FILE_NM = FileUpload1.FileName;
attachment.ATTACH_NM = "Upload test";
attachment.ATTACH_TYPE = FileUpload1.PostedFile.ContentType;
attachment.Save();

Is there a way I can wrap this in a transaction so if attachment.save fails, it should roll back wq.save. Thanks!

I see a SO answer regarding TransactionScope, Can I use this for my case above? Transactions in .net

Community
  • 1
  • 1
WinFXGuy
  • 1,527
  • 7
  • 26
  • 47
  • 1
    What is a `WorkQueue`? Does it handle transactions? If not, then the answer is "no" – John Saunders Jan 16 '14 at 14:30
  • It is simple POCO. It doesn't handle transactions, but I can change the code to implement sql transactions within that class. – WinFXGuy Jan 16 '14 at 14:32
  • "Simple POCO" and "SQL" - I think you need to explain more. Clearly, it's persistent. Please ad some information about these classes to your question. – John Saunders Jan 16 '14 at 14:37
  • WorkQueue and WQAttachment are just two classes with some public properites and a public metod Save. There is nothing special about them. The Save method calls Enterprise Librarry methods to perform the save by calling a stored procedure. – WinFXGuy Jan 16 '14 at 14:45
  • I have little experience doing transactions, but http://msdn.microsoft.com/en-us/library/ms229975.aspx seems like a possible avenue to take. – Aaron Jan 16 '14 at 14:58
  • If you are using Enterprise Library, then check to see if it has a way to handle transactions. The "Enterprise" suggests the answer is "yes". Otherwise, you can probably get away with using [`System.TransactionScope`](http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx) – John Saunders Jan 16 '14 at 15:04
  • @Aaron: no, he does not want to implement a resource manager. That's sort of the inverse of what he wants. – John Saunders Jan 16 '14 at 15:05
  • System.TransactionScope could be the solution to my problem here. I will try an see if it works. Thanks @JohnSaunders – WinFXGuy Jan 16 '14 at 15:10
  • @JohnSaunders: Oh, I must have mis-interpreted the question. I thought the problem was that TransactionScope wouldn't work as is because the WorkQueue and WQAttachment don't support transactions. I assumed TransactionScope would be used in the code above, and figured that link would show how to make whatever database interface you are using compatible with it. – Aaron Jan 16 '14 at 15:20

1 Answers1

0

System.TransactionScope works great for this scenario. Thanks @Aaron and @JohnSaunders

WinFXGuy
  • 1,527
  • 7
  • 26
  • 47