Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7742021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:10:38+00:00 2026-06-01T09:10:38+00:00

I am working on catching concurrency exceptions. I am able to catch the concurrency

  • 0

I am working on catching concurrency exceptions. I am able to catch the concurrency exceptions when a user edits the data fine. I am having trouble catching the exception when a user deletes data.

On my Index page, I have a button to delete each Vehicle object. Pressing that button does a Post to the Delete action. Here is the Delete action:

[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(VehicleIndexViewModel vehicleIndexViewModel)
{
  try
  {
    Vehicle vehicle = db.Vehicles.Find(vehicleIndexViewModel.VehicleID);
    //To test for concurrency errors
    //vehicle.Timestamp = vehicleIndexViewModel.Timestamp;  

    db.Entry(vehicle).State = EntityState.Deleted;
    db.SaveChanges();
    return RedirectToAction("Index");
  }
  catch (DbUpdateConcurrencyException)
  {
    return RedirectToAction("Index", 
      new System.Web.Routing.RouteValueDictionary{{"concurrencyError", true }});
  }
  catch (DataException)
  {
    //Log the error (add a variable name after Exception)
    ModelState.AddModelError(string.Empty, "The system was unable to delete that" 
      + " vehicle. Try again, and if the problem persists"
      + " contact your system administrator.");
    return RedirectToAction("Index");
  }
}

No matter what, the user should be redirected to the Index page. Here is the Index page’s Get action:

public ViewResult Index(bool? concurrencyError)
{
  if (concurrencyError.GetValueOrDefault())
  {
    ViewBag.ConcurrencyErrorMessage = "The record you attempted to delete "
      + "was modified by another user after you got the original values. "
      + "The delete operation was canceled and the current values in the "
      + "database have been displayed. If you still want to delete this "
      + "record, click the Delete button again. Otherwise "
      + "click the Back to List hyperlink.";
  }

  IEnumerable<Vehicle> vehicles = db.Vehicles.Include(v => v.VehicleType);

  IEnumerable<VehicleIndexViewModel> viewModel 
    = Mapper.Map<IEnumerable<Vehicle>, 
                 IEnumerable<VehicleIndexViewModel>>(vehicles);

  return View(viewModel);
}

The code never catches the concurrency error. I test by opening the index page twice. On one of the pages, I open the edit page of a vehicle and change something. Once that’s saved, I go back to the other page and click “Delete.” The Delete action fires, and the vehicle is deleted, but the concurrency error is not caught. You can see where I commented out vehicle.Timestamp = vehicleIndexViewModel.Timestamp;. I thought putting the value of the viewModel back into the actual would raise the error, but it doesn’t work that way either.

I’m sure there’s just something I don’t understand, but what am I doing wrong?


EDIT


Erik Philips found the logic error that I had, but there was another issue that I ran into right away. My ViewModel was not returning the Timestamp data. In fact, the only data it was returning was the VehicleID.

When I tried to add a hidden field to the form, I would get an error. The code just below this would not work:

<input type="hidden" name="Timestamp" value="@item"/>

The Timestamp field needs to be a valid Base-64 string. The error you will get is:

The input is not a valid Base-64 string as it contains a non-base 64 character, more than >two padding characters, or a non-white space character among the padding characters.

This is how I ended up storing the Timestamp value on the view:

<input type="hidden" name="Timestamp" value="@Convert.ToBase64String(item.Timestamp)"/> 

So, my whole Html.BeginForm looks like this:

@using (Html.BeginForm("Delete", "Vehicle",  FormMethod.Post, null))
{                                        
    <input type="hidden" name="VehicleID" value="@item.VehicleID"/>  
    <input type="hidden" name="VehicleName" value="@item.VehicleName"/> 
    <input type="hidden" name="Timestamp" value="@Convert.ToBase64String(item.Timestamp)"/>                                
    <input type="image" src="../../Content/Images/Delete.gif" value="Delete" name="deletevehicle @item.VehicleID" onclick="return confirm('Are you sure you want to delete @item.VehicleName.Replace("'", "").Replace("\"", "")?');"/>  
}  

Although, I really didn’t need to put VehicleName in a hidden field.

Once that was done, all I needed to do was use AutoMapper to map the values back into a vehicle object, set the EntityState to Deleted, and try to SaveChanges;

try
{
    Vehicle vehicle = Mapper.Map<VehicleIndexViewModel, Vehicle>(vehicleIndexViewModel);
    db.Entry(vehicle).State = EntityState.Deleted;
    db.SaveChanges();
    return RedirectToAction("Index");
}
catch (DbUpdateConcurrencyException)
{
    return RedirectToAction("Index", new System.Web.Routing.RouteValueDictionary { { "concurrencyError", true } });
}

That’s it!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T09:10:39+00:00Added an answer on June 1, 2026 at 9:10 am

    Here is what I see would happen if these events happened in this order:

    User 1

    Vehicle/Edit VehicleID=1 (Timestamp =1)

    User 2

    Vehicle/Edit VehicleID=1 (Timestamp =1)

    User 1

    Vehicle/Update VehicleID=1, Title=”Some Text”, (Timestamp=2)

    User2

    Vehicle/DeleteConfirmed VehicleID=1, Timestamp =1

    /* DeleteConfirmed */
    
    // Default Model Binder 
    VehicleIndexViewModel.VehicleID = 1
    VehicleIndexViewModel.Timestamp = 1
    
    Vehicle vehicle = db.Vehicles.Find(vehicleIndexViewModel.VehicleID);
    //vehicle.VehicleID = 1
    //vehicle.Timestamp = 2
    
    // Set to Delete
    db.Entry(vehicle).State = EntityState.Deleted;
    
    // Delete in database
    db.SaveChanges();
    

    Basically all you’re doing is retrieve the Vehicle in it’s current state (updated from user 1) and deleting it. Unless, by some freak chance, someone did an update between Find and SaveChanges there will never be a DbUpdateConcurrencyException.

    What I believe you can do instead would be to Attach the object to the context, delete it, then call SaveChanges().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working some with a database and catching exceptions to check for various conditions.
I'm working on catching a seriously insidious bug that's happening in my code. The
mkdir() is working correctly this question is more about catching an error. Instead of
I am working on application where user invokes a method from UI , on
I am working on catching errors in my app, and I am looking into
I am trying to read file in Haskell with exception catching but cant get
I'm working on a project where I need to be able to run a
I am working on an android client which reads continues stream of xml data
Is there a better approach than trying to write and just catching the exception?
New push developer here. On my first function the try/catch is catching the error

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.