1

I am new in MVC, made an app that works on my local machine (Windows 7 with SQLExpress) but when I deploy live on Windows 2008 R2, SQL 2008 R2 and IIS7, I get this error: "Sorry, an error occurred while processing your request"

Below are snippets of the code in question:

WEB.CONFIG connectionStrings

add name="RentalEntities" connectionString="Data Source=[ServerName];Initial Catalog=[DatabaseName];Integrated Security=True" providerName="System.Data.SqlClient" 

HOMECONTROLLER

namespace Rental.Controllers    
{    
   RentalEntities rentalDB = new RentalEntities();

   ...

   public ActionResult Index()

   {             
      var listings = from l in rentalDB.Listings select l;
      return View(listings.ToList());    
   }
}

HOME/INDEX.CSHTML

@model List<Rental.Models.Listing>

@foreach (var listing in Model)    
{    
    @listing.ListingName <br />     
}

The error occurred on this line @foreach (var listing in Model), something to do with being null - note that NONE of the fields in the database are null. Any help will be appreciated.


Below is the full error message: [ where line 13 is: foreach (var listing in Model)

Sorry, an error occurred while processing your request.

System.NullReferenceException: Object reference not set to an instance of an object. at ASP._Page_Views_Home_Index_cshtml.Execute() in c:\inetpub\wwwroot\projects\Rentals\Views\Home\Index.cshtml:line 13 at System.Web.WebPages.WebPageBase.ExecutePageHierarchy() at System.Web.Mvc.WebViewPage.ExecutePageHierarchy() at System.Web.WebPages.StartPage.ExecutePageHierarchy() at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
jww
  • 97,681
  • 90
  • 411
  • 885
Charlako
  • 31
  • 1
  • 5

1 Answers1

0

What has happened here is that you've passed a null value into the Model for index.cshtml. The code is foreach'ing over null.

Debugging this in Prod:

  • How many rows are in the Listing table on the Prod server?
  • Does your LINQ To SQL .dbml refer exactly to the connection string in your web.config, or does it have a hidden connection string buried within the .designer.cs?
  • Try setting up some debug info on INDEX.CSHTML

    @(Model==null) //will write to page whether the model data being passed is null.
    

Debugging this in Dev/Test:

  • To help debug why it's null, set a breakpoint for yourself on the line in Index(). Set it on return View(listings.ToList());. Is it actually null? Then set a breakpoint on the check for null in your View.

Before foreach, run a check for null:

@if (Model != null)
{
     foreach (var foo in Model)
     {
       //your code
     }
}
else
{
    @: Model Is null!
}
John Gibb
  • 10,603
  • 2
  • 37
  • 48
p.campbell
  • 98,673
  • 67
  • 256
  • 322
  • I did the above if else, and it did not like that part @"Model Is null!". When I removed it, it runs and shows nothing, which means the Model is null. Why is that? I have set the database and entities properly... – Charlako Apr 15 '11 at 18:18
  • @Charl: see the update about your .dbml. I'd assumed you're using LINQ To SQL and not EF. – p.campbell Apr 15 '11 at 18:39
  • can you please elaborate on your last comment please, I don't understand what you mean when you say "see the update about your .dbml". Like you can see on my controller above, I used entities: RentalEntities rentalDB = new RentalEntities(); All of this worked on my dev and my friends computer locally... I am very new with MVC. – Charlako Apr 15 '11 at 18:47
  • @Char: I meant that I'd updated my answer to include a bullet about .dbml. It's all about what connection string your .dbml is using. Check out your .dbml.designer.cs file. Does it have a connection string buried in the first constructor? See here: http://i.imgur.com/wbu0a.png – p.campbell Apr 15 '11 at 20:19