Sunday, 8 September 2013

When i try to execute foreach statement i am getting -Object reference not set to an instance of an object

When i try to execute foreach statement i am getting -Object reference not
set to an instance of an object

i am working on mvc project. In my controller i am calling my stored
procedure from terms class and I am returning Index page if it returns
true or return terms page if it returns false.
Calling stored procedure in terms page :
public class Accept
{
public void Check()
{
using (var ctx = new termsEntities())
{
ctx.usp_ChkTerms(8, new ObjectParameter("Accepted",
typeof(bool)));
ctx.SaveChanges();
}
}
}
Now i am calling this in my controller :
public ActionResult App()
{
// calling Stored procedure from Model to class
var accept = new Accept();
accept.Check();
// checking if accepted is true then return view else return another view
AppEntities Accepted = new AppEntities();
AppTerm user = new AppTerm();
AppHist history = new AppHist();
user = (from AppTerm app in Accepted.AppTerms
where app.userID == 8
select app).ToList().FirstOrDefault();
if (user != null)
{
if (user.Accepted)
{
return View("Index");
}
else
{
return View("terms");
}
}
And this is the code i am using in my terms view :
@{
ViewBag.Title = "terms";
}
<html>
<body>
<ul>
@foreach ( var item in Model)
{
<div class="Page" onclick="location.href='@Url.Action("Info", new {
id = item.ID })'">
span class="Col1">
<br />
@item.ID
</span>
<span class="Title">@item.Name</span>
}
</ul>
</body>
</html>
Here when condition is true it is displaying Index page but when condition
falls and when it tries to display terms page i am getting Object
reference not set to an instance of an object and error is pointing to
foreach loop. so what mistake i am doing here? i need help..

No comments:

Post a Comment