1

I'm getting an error when recalling some data I've just updated to the database from using a form.

I'm using Visual Studio Express 2010 MVC3 Razor (and I'm very new to all forms of development). I've filled out the form, clicked the update button which is successfully storing the text in the textboxes to the table, but then when I try to view that data on a seperate page I get this error:

NullReferenceException was unhandled by user code.
Object reference was not set to an instance of an object.

I'm sure this is obvious to most, but I'm unsure what I've done wrong.

Appreciate any noob help please, many thanks.

==================================================================================

the code for the "View" is here:

@model IEnumerable<bhgc.Models.Data>


@{
ViewBag.Title = "Special Offers";
}

<fieldset>
<legend><h1>Special Offers</h1></legend>
@foreach (var data in Model)
{
<table>
<tr>
    <td>
        <strong>
             @Html.Raw(data.offer1.Replace(Environment.NewLine, "<br/>"))
        </strong>
    </td>
    <td>
        <strong>
             @Html.Raw(data.offer2.Replace(Environment.NewLine, "<br/>"))
        </strong>
    </td>
</tr>
<tr>    
    <td>
        <strong>
             @Html.Raw(data.offer3.Replace(Environment.NewLine, "<br/>"))
        </strong>
    </td>    
</tr>
</table>
}

</fieldset>

==================================================================================

and the controller is here:

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

namespace bhgc.Controllers
{
public class HomeController : Controller
{
    private DataDBContext db = new DataDBContext();
    public ActionResult Index()
    {
        return View(db.Data.ToList());
    }

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

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

==================================================================================

Controller that reads from database:

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

namespace bhgc.Controllers
{
[Authorize] //Created Validataion so inaccessible from outside
public class DataController : Controller
{
    private DataDBContext db = new DataDBContext();

    //
    // GET: /Data/

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

    //
    // GET: /Data/Details/5

    public ViewResult Details(string id)
    {
        Data data = db.Data.Find(id);
        return View(data);
    }



    //
    // GET: /Data/Update

    public ActionResult Update()
    {
        var model = db.Data.FirstOrDefault();
        return View(model);
    }

    //
    // POST: /Data/Update

    [HttpPost]
    [ValidateInput(false)]
    public ActionResult Update(Data data)
    {
        if (ModelState.IsValid)
        {
            data.ID = 1; //EF need to know which row to update in the database.       
            db.Entry(data).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Special", "Home");
        }
        return View(data);
    }


}
}
4
  • 2
    I would assume that db.Data is null - have you used the debugger to find out exactly which variable is null and where?. I have no idea why, because you haven't shown the code that reads from the database. Commented Apr 9, 2012 at 21:03
  • 1
    I'd recommend that you do some reading on layered programming and data access layer in particular. That code just doesn't look right in any way to me. Commented Apr 9, 2012 at 21:06
  • 2
    Yikes! Unmanaged database connection :( always close your DBContext explicitly. Either through a using block or with myContextName.Dispose(). Moreover, sending that entire context to the view is definitely not advised. Look into "view models" in mvc3. Commented Apr 9, 2012 at 21:08
  • Thanks guys, I'll research what I can on the Unmanaged database connection. As said, am early stages of learning. Commented Apr 9, 2012 at 21:22

1 Answer 1

1

db.Data may be null. Check that it is returning data. However, your foreach could handle that. Also consider that offer1, offer2, or offer3 may be null. Calling Replace on a null object would also throw that error.

Also, please heed the advice of people commenting on your code. There are some very dangerous habits in effect.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for that - I opened the physical database to check that the table had data in each field and it indeed has: zen87038.zen.co.uk/dl/data_table.JPG

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.