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

  • Home
  • SEARCH
  • 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 7777039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:02:15+00:00 2026-06-01T18:02:15+00:00

I’m getting an error when recalling some data I’ve just updated to the database

  • 0

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);
    }


}
}
  • 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-01T18:02:17+00:00Added an answer on June 1, 2026 at 6:02 pm

    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.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a text area in my form which accepts all possible characters from
I have some data like this: 1 2 3 4 5 9 2 6
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.