10 Replies - 7120 Views - Last Post: 30 November 2012 - 06:33 PM

#1 gregwhitworth  Icon User is offline

  • Tired.
  • member icon

Reputation: 219
  • View blog
  • Posts: 1,604
  • Joined: 20-January 09

Error on save effecting 0 rows

Posted 18 November 2012 - 08:41 PM

I have a simple web application using MVC4 and created a very simple model and created the CRUD. The add and delete capabilties work fine. When editing though I get the following error:

Quote

Store update, insert, or delete statement affected an unexpected number of rows (0).


I looked at this Stack Overflow issue with the same problem and they suggest it's because that there are people overwriting the same row at the same time. I am in a dev environment and I am the only user using this.

Just in case, here is my model:

using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web;
using System.Web.Mvc;

namespace ArtAide.Models
{
    public class Location
    {
        public int LocationId { get; set; }
        public string Name { get; set; }
    }
}



Thanks for the help.

Is This A Good Question/Topic? 0
  • +

Replies To: Error on save effecting 0 rows

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: Error on save effecting 0 rows

Posted 19 November 2012 - 04:05 AM

Think I'll move this to ASP.NET's MVC sub-forum.
Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4437
  • View blog
  • Posts: 7,713
  • Joined: 08-June 10

Re: Error on save effecting 0 rows

Posted 19 November 2012 - 07:18 AM

The Model isn't where the problems are going to be. Can you show us the code you use to actually perform the updates?
Was This Post Helpful? 0
  • +
  • -

#4 gregwhitworth  Icon User is offline

  • Tired.
  • member icon

Reputation: 219
  • View blog
  • Posts: 1,604
  • Joined: 20-January 09

Re: Error on save effecting 0 rows

Posted 19 November 2012 - 11:02 AM

Sure, here is the Controller code for the edit methods:

//
        // GET: /Location/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Location location = db.Locations.Find(id);
            if (location == null)
            {
                return HttpNotFound();
            }
            return View(location);
        }

        //
        // POST: /Location/Edit/5

        [HttpPost]
        public ActionResult Edit(Location location)
        {
            if (ModelState.IsValid)
            {
                db.Entry(location).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(location);
        }



It is occurring on the db.SaveChanges(); method.

Greg
Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4437
  • View blog
  • Posts: 7,713
  • Joined: 08-June 10

Re: Error on save effecting 0 rows

Posted 19 November 2012 - 12:31 PM

What happens if you comment out the line immediately above it, line 22?
Was This Post Helpful? 0
  • +
  • -

#6 gregwhitworth  Icon User is offline

  • Tired.
  • member icon

Reputation: 219
  • View blog
  • Posts: 1,604
  • Joined: 20-January 09

Re: Error on save effecting 0 rows

Posted 19 November 2012 - 10:21 PM

That makes the error disappear but it doesn't save it. Which doesn't make much sense because there isn't a conditional before that.
Was This Post Helpful? 0
  • +
  • -

#7 gregwhitworth  Icon User is offline

  • Tired.
  • member icon

Reputation: 219
  • View blog
  • Posts: 1,604
  • Joined: 20-January 09

Re: Error on save effecting 0 rows

Posted 19 November 2012 - 11:10 PM

Ok. Still learning my way around debugging in here, but I think I may have found the possible issue here, upon debugging the location object looks like so:

Posted Image

But when looking at the table data you get this:

Posted Image

There is no ID with 0. How can this be happening seeing as how this was built by the scaffolding of the model. I know the default of the parameter id is 0, but I would assume that the GET values are 1 or higher. Thanks for the help!
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4437
  • View blog
  • Posts: 7,713
  • Joined: 08-June 10

Re: Error on save effecting 0 rows

Posted 20 November 2012 - 08:23 AM

I think I know. Let's see your cshtml to be sure, but I think the issue is that you're not using the ID in the form. If you're not using an Editor for a field, but you still want it to be serialized as part of your model in the post, you need to have a HiddenFor that field.
Was This Post Helpful? 1
  • +
  • -

#9 gregwhitworth  Icon User is offline

  • Tired.
  • member icon

Reputation: 219
  • View blog
  • Posts: 1,604
  • Joined: 20-January 09

Re: Error on save effecting 0 rows

Posted 22 November 2012 - 11:35 AM

Ok here is the Edit CSHTML, I'm still kind of amazed that this isn't working right out of the box since it is the default build from the scaffolding. I must have put in a validator that broke this part when building.

@model ArtAide.Models.Location

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Location</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}


Was This Post Helpful? 0
  • +
  • -

#10 gregwhitworth  Icon User is offline

  • Tired.
  • member icon

Reputation: 219
  • View blog
  • Posts: 1,604
  • Joined: 20-January 09

Re: Error on save effecting 0 rows

Posted 23 November 2012 - 03:37 PM

Ok. So I deleted my controller and views, made the model at it's simplest form and then recreated it. This time it worked. It added in the following:

@Html.HiddenFor(model => model.LocationId)


Which it wasn't before.

Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

#11 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4437
  • View blog
  • Posts: 7,713
  • Joined: 08-June 10

Re: Error on save effecting 0 rows

Posted 30 November 2012 - 06:33 PM

Sorry it's taken me so long to get back to this thread, was out for a surgery. Yeah, looks like that was it, missing an ID. The way MVC works is that it will try to reconstruct POST parameters as properties of your model parameter. If it's not included in the post, it won't be deserialized into the object. That's why you have to include a hidden, because that will come across as a field parameter. Any values that are not included in the post parameters will be set to their default values, which for an int is zero.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1