0

Hi friends I have a table called Console and inside it contains one attribute called ConsoleName this is the ID of this table and the foreign key of two tables called Game and `Gamer. The problem that I am having is when I run the application everything works fine and I can insert all fine but when it comes to deleting a CONSOLE I cant delete any record when from the Console table as it gives me the following error:

Value cannot be null.
Parameter name: entity

I am using MVC3 C#

I tried the FOLLOWING:

[HttpPost, ActionName("Delete")]
    public ActionResult DeleteConfirmed(string id?)
    {            
        ConsoleTBL consoletbl = db.ConsoleTBLs.Find(id?);
        db.ConsoleTBLs.Remove(consoletbl);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

But it does not work.

Please if you need anything let me know an I shall post what you ask for thanks

EDIT: Console Controller:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
{ 
    public class ConsoleController : Controller
    {
        private GameZoneEntities3 db = new GameZoneEntities3();

        //
        // GET: /Console/

        public ViewResult Index()
        {
            return View(db.ConsoleTBLs.ToList());
        }

        //
        // GET: /Console/Details/5

        public ViewResult Details(string id)
        {
            ConsoleTBL consoletbl = db.ConsoleTBLs.Find(id);
            return View(consoletbl);
        }

        //
        // GET: /Console/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Console/Create

        [HttpPost]
        public ActionResult Create(ConsoleTBL consoletbl)
        {
            if (ModelState.IsValid)
            {
                db.ConsoleTBLs.Add(consoletbl);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(consoletbl);
        }

        //
        // GET: /Console/Edit/5

        public ActionResult Edit(string id)
        {
            ConsoleTBL consoletbl = db.ConsoleTBLs.Find(id);
            return View(consoletbl);
        }

        //
        // POST: /Console/Edit/5

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

        //
        // GET: /Console/Delete/5

        public ActionResult Delete(string id)
        {
            ConsoleTBL consoletbl = db.ConsoleTBLs.Find(id);
            return View(consoletbl);
        }

        //
        // POST: /Console/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {            
            ConsoleTBL consoletbl = db.ConsoleTBLs.Find(id);
            db.ConsoleTBLs.Remove(consoletbl);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}

Index page of Console table:

@model IEnumerable<MvcApplication1.Models.ConsoleTBL>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            ConsoleID
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.ConsoleID)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
        </td>
    </tr>
}

</table>

I have set the primary key on the actionlinks to "ConsoleID" but this did not work either just to tell you know what I have done

user1137472
  • 345
  • 2
  • 5
  • 20

2 Answers2

0

From the sound of it if you delete a Console you would orphan records in the Game and Gamer tables (which is not allows when a Foreign Key is enforced). So in order to delete a Console you'd either have to delete the other records using it as a foreign key or set the cascading behavior to do it for you.

Edit: However the errors you posted seems to imply you are missing a parameter. For deletes you will likely have two actions called Delete with one View. one Delete action will be a normal page request (typically to confirm the deletion) and the other will be the one requiring the postback (which you posted in your question).

The process will likely be something like this

Get /consoles/details/1

click the delete button

Get /consoles/delete/1

Press confirm or OK (which will be a submit button in a form)

Post /consoles/delete/1

Nick Albrecht
  • 16,607
  • 10
  • 66
  • 101
  • I dont think that is case because I have tried that, i have deleted all dependencies and tried to delete the parent (console) but it still does not delete. – user1137472 Mar 21 '12 at 22:51
0

Did you run this in debug mode and step into the delete method?

Did you check to make sure the consoletbl exists?

[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(string id)
{            
    ConsoleTBL consoletbl = db.ConsoleTBLs.Find(id);

    if( consoletbl != null)
    {
        db.ConsoleTBLs.Remove(consoletbl);
        db.SaveChanges();
    }
    else
    {
        // should probably return an error message
        throw new ArgumentNullException("Could not find a console with the id of " + id);
    }
    return RedirectToAction("Index");
}

This should throw an exception if the id you are passing in is not found...

@model IEnumerable<MvcApplication1.Models.ConsoleTBL>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            ConsoleID
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.ConsoleID)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new {  id=item.ConsoleID}) |
            @Html.ActionLink("Details", "Details", new {  id=item.ConsoleID}) |
            @Html.ActionLink("Delete", "Delete", new {  id=item.ConsoleID})
        </td>
    </tr>
}

</table>

I assume ConsoleID is the primary id so I replace "PrimaryKey" with this property. /* */ are used to put comments in your code and anything in between them will not be compiled

clyc
  • 2,420
  • 14
  • 15
  • Yes console table exists and yes i did run it in debug and no it did not help as soon as i run it and access the index page of console and try to delete a console i get the error that i have mentioned – user1137472 Mar 21 '12 at 23:24
  • I have added your code and there is a change, when I know try to press delete nothing happens, the page refreshes it self back and the record is still there but it does not produce the error and crash like it use to – user1137472 Mar 21 '12 at 23:28
  • That means that there is no consoletbl item with the id that you passed into the method. – clyc Mar 21 '12 at 23:30
  • The table exists there are two records within it, PS3, XBOX I am viewing them in my server explorer as we speak – user1137472 Mar 21 '12 at 23:33
  • @user1137472 added code to just throw an exception with the id you are passing in to show you that this is what is causing the problem – clyc Mar 21 '12 at 23:36
  • I tried you edited code and when i run it and try to delete i still get my original error your message does not appear – user1137472 Mar 21 '12 at 23:40
  • @user1137472 when you stepped through your code, what was the content of the id variable? – clyc Mar 21 '12 at 23:42
  • In my table i have two ID's both set as CHAR which are XBOX and PS3 when i ran your edited code i got the following error: Value cannot be null. Parameter name: Could not find a console with the id of (Thats all i get) – user1137472 Mar 21 '12 at 23:44
  • @user1137472 that means that you are not passing in the id to the delete method. since the default value of a string is empty, your code is trying to look for a console with an id of "". You need to look at how you are calling the delete method on your page. – clyc Mar 21 '12 at 23:45
  • I have been at this for ages I do not not how to fix this error is there any help you can give? to fix this thanks – user1137472 Mar 21 '12 at 23:45
  • I have set the table NOT to allow NULLS – user1137472 Mar 21 '12 at 23:46
  • @user1137472 post the html of the page (view) you are using. – clyc Mar 21 '12 at 23:48
  • Thanks I shall, I will edit my question above for what you request – user1137472 Mar 21 '12 at 23:49
  • I have replaced the "PrimaryKey" with the id which is ConsoleID and i get the following error on my browers: Server Error in '/' Application. The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly. Requested URL: /console/Edit/PS3 – user1137472 Mar 21 '12 at 23:58
  • @user1137472 but does the delete work? Also... debug and see if it's even going to the Edit method. – clyc Mar 22 '12 at 00:07
  • I did a debug and when i try to delete or edit it shows that error that I have posted in your answer – user1137472 Mar 22 '12 at 00:13
  • @user1137472 but.. did you copy my code and replace your view page with it? If you didn't, you need to get rid of the /* and */ in the ActionLink portion of the code.. – clyc Mar 22 '12 at 00:14
  • Yes i have done this, and when i run and get the browser error, out of curiosity i put my mouse's curser into the the address bar and pressed enter and the delete part came up and I can delete, but I dont now why that error is showing every time i press delete on debug – user1137472 Mar 22 '12 at 00:18
  • Can you help me on why I am getting that browser error thanks – user1137472 Mar 22 '12 at 00:44