Note that there are some explanatory texts on larger screens.

plurals
  1. POEnsure 2 methods succeed else roll back
    primarykey
    data
    text
    <p>Can anyone please tell me whether this is possible. </p> <p>I have some code that allows a user to upload/change their image, before the change takes place I delete the default/old image from disk before uploading new image.</p> <p>Problem is if something goes wrong either with the delete or upload, how can I roll back both so that the original image is returned. </p> <p>I thought I could use tranactionscope, but either I'm not using it correctly or it not applicable for this case.</p> <p>All the examples I have found involve using 2 call to database, but my code only involves one call and that's to update.</p> <pre><code>//TODO check transactionscope works ok using (var tran = new TransactionScope()) { //Delete old image before updating new image //123 bogus number to throw error var deleteOldImage = _igmpfu.DisplayProfileDetailsForUpdate("123") .FirstOrDefault(); if (Convert.ToString(deleteOldImage) != "5bb188f0-2508-4cbd-b83d-9a5fe5914a1b.png") { DeleteOldImage(deleteOldImage); } //Insert new image var imageGuid = imageId + ".png"; bool imageUrl = _iuma.UpdateAvatar(cookieId, imageGuid); if (imageUrl) { TempData["Message"] = "Image updated"; return RedirectToAction("Index", "Members"); } tran.Complete(); } </code></pre> <p>Any assistance in helping a newbie would be appreciated</p> <p>//------------------------</p> <p>I have been looking at the computer to long, all I had to do was </p> <pre><code>var deleteOldImage = _igmpfu.DisplayProfileDetailsForUpdate("123").FirstOrDefault(); if (deleteOldImage != null) { code here for writing to disk } </code></pre> <p>I have spent ages trying to work this out and thats all I had to do :-(</p> <p>Thanks everyone for their replies.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload