ViewModel Delete Concurrency Excecption Not Working - asp.net-mvc-3

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!

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().

Related

To create a session explicitly for first the first time page loads in Grails

As soon as a page loads, it renders a lot of data and shows on the view which sort of slows down the performance. I want to restrict this and load the data only when a filter is applied .
I need a way in which a session variable can store the value on the 1st login and no data should be loaded in that 1st session i.e. when any user loads it for the very first time using his login. something like the below in the controller class:
if(session.dtstartDate && session.dtstartDate != '')
{
SimpleDateFormat nsdf = new SimpleDateFormat('yyyy-MM-dd')
Date startDateValue = nsdf.parse(session.dtstartDate.substring(0, session.dtstartDate.lastIndexOf("T")))
eq("startDate", startDateValue)//if any filter is applied
}
else{
if this is the 1st session the startdate should be null --> need a piece of code to be replaced here
}
I am unsure maybe I still have not got what you have tried to ask properly, I thought I should try to answer your question to how I understood your problem to be.
If we take this basic filter and add some stuff to it we may be able to get to what you wish to do using a better method ? I am unsure what startDate is actually representing but if we base it on if a user has hit a controller for the first time or not the answer would be something like this, you could replace the logic to startDate if it has other significance:
so adding some hashmap arrayset to your filter that gets called before the action is called when user clicks the controller/action:
in your conf/MyFiters.groovy
class MyFilters {
static final Set<HashMap<String[],String[]>> userControl =
([:] as Set).asSynchronized()
//where controller is controllerName and action is actionName
def filters = {
MyLogger() {
before = {
if (verifyClientMaster(session.id as String ,controllerName)==false) {
clientMaster.add(session.id as String:controllerName)
// now here you have a new user so set
// some session value for gsp or load something
//according
}else{
// user has hit it before do something else or set something else
}
}
}
}
}
Boolean verifyClientMaster(String sessionId,String controller) {
// iterate
boolean found = false
userControl.each { k,v -> if (k == sessionId && v == controller) {
found = true
}
}
}
something like this and you know if the user has hit the controller or not.. remember the session is per user. so a new user has a new session entity.
Hope it is of help and not off track..
E2A
Thinking about it you do go down this route then you would need to keep track of when session expires and to remove the user from clientMaster.. take a look at this project if you did go down this route.. personally I would even do it simpler than this... on a rethink...
class MyFilters {
def filters = {
MyLogger() {
before = {
if (!sessions."${controllerName}") {
sessions."${controllerName}"="${controllerName}"
// now here you have a new user so set
// some session value for gsp or load something
//according
}else{
// user has hit it before do something else or set something else
}
}
}
}
}
and even simple than any of this would be to use the intelligence built into a gsp if what you load can be based on it... (not tested any of it ha)
<g:if test="${!session."${controllerName}"}">
<g:set var="${controllerName}" value="${controllerName}" scope="session" />
<g:render template="firstTimeHitter"/>
</g:if>
<g:else>
<g:render template="secondTimeHitter"/>
</g:else>
or just your controller that checks and sets that and either renders something different or sets something gsp picks up on..
def myController {
def doSomething() {
boolean firstTime = false
if (!session."${controllerName}") {
// first time either render or set firsTime
firstTime = true
session."${controllerName}" = controllerName // or startDate
// render view: 'firstTime, model: [firstTime:firstTime, params:params]
} else{
// render view: 'firstTime, model: [firstTime:firstTime, params:params]
}
// if no render above:
render view: 'doSomething, model: [firstTime:firstTime, params:params]
// now in doSomething gsp you look for firstTime:
}
do someThing:
<g:if test="${firstTime.toString().equals('true')}">
<g:render template="firstTimeHitter"/>
</g:if>
<g:else>
<g:render template="secondTimeHitter"/>
</g:else>
The possibilities are endless, the differences being with a filter its a one fits all, i.e. it is checking every controller as it is hit by each user. In controller and gsp solution you have to declare it where needed. You could have an abstract controller that other controllers extend to repeat that check as a higher class that gets called to verify, regardless their all a lot more repetitive than a simple one off filter...
Final Edit to give other other alternatives would be:
final Set<Session> jsessions = ([] as Set).asSynchronized()
jsessions.add('controller1')
jsessions.add('controller2')
jsessions.add('controller3')
jsessions.add(controllerName)
println "=== ${jsessions}"
if (jsessions.contains(controllerName)) {
println "--- We have ${controllerName} defined in our session set.... jsessions"
}
ArrayList jsessions2 = []
jsessions2.add(controllerName)
session.jsessions2 = jsessions2
//repeat this on every call
ArrayList jsessionsret = session.jsessions2
jsessionsret.add('controller1')
jsessionsret.add('controller2')
jsessionsret.add('controller3')
session.jsessions2 = jsessionsret
if (jsessions2.contains(controllerName)) {
println "--- We have ${controllerName} defined in our session set.... jsessionsret"
}
println "222 --- ${jsessions2}"
This segment above are two different implementations of using first a session set that is global and could be used if you do not care if the controller is hit by usera userb etc so if usera hits it userb would also be considered as hitting it.. This is jsessions.
The bottom jsessions2 is an attempt to turn a single session key into an ArrayList. So rather than storing lots of single object i.e. session."${controllerName}" per call of a controller per user session. You could have 1 single session key per user that you append each controller they hit to.. and you then check to see if they have that controller

MS Dynamics CRM Form Field OnChange check for Existing Value in Entity

Unless someone can explain what I'm missing, CRM 2013 does not have any way to check for a duplicate WHILE entering a new Lead record. I want to check for a duplicate BEFORE the new record is saved. I can't seem to figure this one out.
Basically, when a user enters the Company Name on a new Lead record, I'd like JavaScript or something check for the existence of that value in all the other Lead records and return True or False. That way I can alert the user that the Company already exists BEFORE they save the new record.
Make sense? Am I just TOTALLY missing something here?
Thanks,
Scotty
Microsoft removed this functionality. But you can restore it using one of following articles:
http://a33ik.blogspot.com/2013/10/how-to-turn-on-duplicate-detection-for.html
http://jlattimer.blogspot.com/2013/10/are-you-missing-duplicate-detection-in.html
You can use below function to check duplicate records and set alert/field value depending upon result set :
CheckDuplicate: function (someIdentifier) {
var value = null;
var filter = "?$select=*&$filter=(new_Identifier eq '" + someIdentifier + "') and (new_someGuidField/Id eq guid'" + Xrm.Page.getAttribute("new_someGuidField").getValue()[0].id + "')";
retrieveMultipleSync("new_EntityNameSet", filter, function (data, textStatus, XmlHttpRequest) {
if (data != null && data.length > 0) {
value = data;
}
}, null);
return value;
}

MVC3 Entity Framework Code First Updating Subset Related List of Items

I have a table of data with a list of key value pairs in it.
Key Value
--------------------
ElementName PrimaryEmail
Email someemail#gmail.ca
Value Content/Images/logo-here.jpg
I am able to generate new items on my client webpage. When, I create a new row on the client and save it to the server by executing the following code the item saves to the database as expected.
public ViewResult Add(CardElement cardElement)
{
db.Entry(obj).State = EntityState.Added;
db.SaveChange();
return Json(obj);
}
Now, when I want to delete my objects by sending another ajax request I get a failure.
public void Delete(CardElement[] cardElements)
{
foreach (var cardElement in cardElements)
{
db.Entry(cardElement).State = EntityState.Deleted;
}
db.SaveChanges();
}
This results in the following error.
Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.
I have tried other ways of deleting including find by id remove and attach and delete but obviously I am approaching in the right fashion.
I am not sure what is causing your issue, but I tend to structure my deletes as follows:
public void Delete(CardElement[] cardElements)
{
foreach (var cardElement in cardElements)
{
var element = db.Table.Where(x => x.ID == cardElement.ID).FirstOrDefault();
if(element != null)
db.DeleteObject(element);
}
db.SaveChanges();
}
although I tend to do database first development, which may change things slightly.
EDIT: the error you are receiving states that no rows were updated. When you pass an object to a view, then pass it back to the controller, this tends to break the link between the object and the data store. That is why I prefer to look up the object first based on its ID, so that I have an object that is still linked to the data store.

EntitySqlException when trying to access DbUpdateConcurrencyException's GetDatabaseValues() entity

I'm trying to do the tutorial here: http://www.asp.net/entity-framework/tutorials/handling-concurrency-with-the-entity-framework-in-an-asp-net-mvc-application
In the ActionResult Edit, I have the following code:
public ActionResult Edit(Product product)
{
try
{
if (ModelState.IsValid)
{
db.Entry(product).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
}
catch(DbUpdateConcurrencyException ex)
{
var entry = ex.Entries.Single();
var databaseValuesObj = entry.GetDatabaseValues().ToObject();
var databaseValues = (Product)databaseValuesObj;
var clientValues = (Product)entry.Entity;
if (databaseValues.Name != clientValues.Name)
ModelState.AddModelError("Name", "Current value: "
+ databaseValues.Name);
if (databaseValues.Description != clientValues.Description)
ModelState.AddModelError("Description", "Current value: "
+ String.Format("{0:c}", databaseValues.Description));
if (databaseValues.ControllingStudentId != clientValues.ControllingStudentId)
ModelState.AddModelError("ControllingStudentId", "Current value: "
+ String.Format("{0:d}", databaseValues.ControllingStudentId));
ModelState.AddModelError(string.Empty, "The record you attempted to edit "
+ "was modified by another user after you got the original value. The "
+ "edit operation was canceled and the current values in the database "
+ "have been displayed. If you still want to edit this record, click "
+ "the Save button again. Otherwise click the Back to List hyperlink.");
product.Timestamp = databaseValues.Timestamp;
}
catch (DataException)
{
//Log the error (add a variable name after Exception)
ModelState.AddModelError(string.Empty, "Unable to save changes. Try again, and if the problem persists contact your system administrator.");
}
return View(product);
}
On the var databaseValuesObj = entry.GetDatabaseValues().ToObject(); line, I get an exception like this:
System.Data.EntitySqlException was unhandled by user code
Message=Type 'MvcApplication3.DAL.Product' could not be found. Make sure that the required schemas are loaded and that the namespaces are imported correctly. Near type name, line 1, column 119.
Source=System.Data.Entity
Column=119
ErrorContext=type name, line 1, column 119
ErrorDescription=Type 'MvcApplication3.DAL.Product' could not be found. Make sure that the required schemas are loaded and that the namespaces are imported correctly.
Line=1
...
My question is, how can I show it where the Product class is? Its in the project and I've got the using statement at the top. Why can't it find it?
Edit:
Based on the response below, I changed my code to:
var entry = ex.Entries.Single();
var currentValues = entry.CurrentValues.Clone();
entry.Reload();
entry.CurrentValues.SetValues(currentValues);
var clientValues = (Product)entry.Entity;
var databaseValues = (Product)entry.OriginalValues.ToObject();
And that seemed to fix it. But I think it will have issues if the row is deleted. My current problem won't have that issue, so this is a good fix for me. Thanks!
This is a known issue when the context is in a different projects. No workarounds currently exist except for moving the context into the same project.
http://social.msdn.microsoft.com/Forums/en-HK/adodotnetentityframework/thread/fa67aa0e-3bca-44a5-9e00-af6362a539a7
EDIT
Actually I take that back - there is a workaround now listed there since the last time I read this. cool : )

Using Cache in Play Framework

I'm trying to implement a quiz application. The application loads the questions with ajax one by one. When a user clicks the 'to next question' button his/her answer is saved in cache. But when I debug, cache list is always null...
This code creates the first cache array:
public static void viewQuiz(#Required String user, #Required String test) {
if(validation.hasErrors()) {
flash.error("Hoop kullanıcı lazım…");
index();
} else{
TestClass selectedTest = TestClass.find("title", test).first();
List<String> choiceList = new ArrayList<String>();
session.put("testID", selectedTest.id);
Cache.set("choices", choiceList, "30mn");
render();
}
}
And this code is trying to save the answers one by one:
public static void question(#Required Long id, String answer){
Long testId = Long.parseLong(session.get("testID"));
TestClass test = TestClass.findById(testId);
List<Question> questList = Question.find("test_id", test.id.intValue()).fetch();
Question quest = questList.get(id.intValue());
if(answer != null){
List<String> choiceList= Cache.get("choices",List.class);
choiceList.add(id.intValue(), answer);
Cache.set("choices", choiceList, "30mn");
}
int count = questList.size()-1;
render(quest, count, id);
}
And this code is the html view of the second:
#{extends 'main.html' /}
#{set title:'question.html' /}
<script type="text/javascript">
var questionId = ${id};
$('#nextButton').click(function(){
$('#questionDiv').html('<p><img id = "loaderGif" src="public/images/loading.gif"/></p>');
$('#questionDiv').load("/test/" + ++questionId);
});
$('#endButton').click(function(){
$('#questionDiv').html('<p><img id = "loaderGif" src="public/images/loading.gif"/></p>');
$('#questionDiv').load("/result");
});
</script>
<legend>Soru ${id+1}</legend>
<p>&{quest.question}</p>
#{list items:quest.choices, as:'choice'}
<p><input type="radio" name = "answer" id = "answer" size="30" value="${choice}"/>&{choice}</p>
#{/list}
#{if id < count}
<input id = "nextButton" name="nextButton" type="button" value="İleri"/>
#{/if}
#{else}
<input id = "endButton" name="endButton" type="button" value="Bitti"/>
#{/else}
Don't use the cache to 'store' objects. Either store it in the session or create a new model to store the answers. Generally, you cannot expect the cache to retain the objects you put into; it's a cache, not a store.
To quote from the Play! website: http://www.playframework.org/documentation/1.2.2/cache
It is important to understand that the cache contract is clear: when
you put data in a cache, you can’t expect that data to remain there
forever. In fact you shouldn’t. A cache is fast, but values expire,
and the cache generally exists only in memory (without persistent
backup).
Cache is not reliable and you may get it as null in dev mode. This is expected and you can try changing it to prod mode and see its behavior.

Resources