session id is in layout. when i login and land in Index Page every thing is fine got all reacquired data username, image, and view data but after that i go to different action method getting the error
Object reference not set to an instance of an object.
Server Error in '/carsEstore' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 29: <ul class="nav navbar-nav navbar-right" style="color:white">
Line 30: <li>
Line 31: <b>@Session["AdminName"]</b>
Line 32: <img src="@Url.Content("~/Content/img/employee/" + System.IO.Path.GetFileName(Model.FileName))" alt="" class="userImg" />
Line 33: </li>
admin layout
@if (HttpContext.Current.Session["AdminID"] != null && HttpContext.Current.Session["AdminID"].ToString() != "")
{
using (Html.BeginForm("LogOff", "Home", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
{
<ul class="nav navbar-nav navbar-right" style="color:white">
<li>
<b>@Session["AdminName"]</b>
<img src="@Url.Content("~/Content/img/employee/" + System.IO.Path.GetFileName(Model.FileName))" alt="" class="userImg" />
</li>
<li>@Html.ActionLink("Log Out", "LogOut", "AdminAccount", routeValues: null, htmlAttributes: new { id = "registerLink" })</li>
</ul>
}
}
else
{
<ul class="nav navbar-nav navbar-right">
<li>@Html.ActionLink("Register", "Register", "Home", routeValues: null, htmlAttributes: new { id = "registerLink" })</li>
<li>@Html.ActionLink("Log in", "Login", "Home", routeValues: null, htmlAttributes: new { id = "loginLink" })</li>
</ul>
}
[HttpPost]
public ActionResult Banner(Banner banner, HttpPostedFileBase file)
{
if (Session["AdminName"] != null)
{
if (ModelState.IsValid)
{
using (CarsStoreDbContext db = new CarsStoreDbContext())
{
if (file != null)
{
if (file.ContentLength > 0)
{
if (Path.GetExtension(file.FileName).ToLower() == ".jpg"
|| Path.GetExtension(file.FileName).ToLower() == ".png"
|| Path.GetExtension(file.FileName).ToLower() == ".gif"
|| Path.GetExtension(file.FileName).ToLower() == ".jpeg")
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/Content/img/employee"), file.FileName);
banner.imgFileName = file.FileName;
file.SaveAs(path);
}
}
}
db.banner.Add(banner);
db.SaveChanges();
}
ModelState.Clear();
}
return View();
}
else
{
return RedirectToAction("Index", "AdminAccount");
}
}
What I have tried:
<pre lang="c#">
admin layout
@if (HttpContext.Current.Session["AdminID"] != null && HttpContext.Current.Session["AdminID"].ToString() != "")
{
using (Html.BeginForm("LogOff", "Home", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
{
<ul class="nav navbar-nav navbar-right" style="color:white">
<li>
<b>@Session["AdminName"]</b>
<img src="@Url.Content("~/Content/img/employee/" + System.IO.Path.GetFileName(Model.FileName))" alt="" class="userImg" />
</li>
<li>@Html.ActionLink("Log Out", "LogOut", "AdminAccount", routeValues: null, htmlAttributes: new { id = "registerLink" })</li>
</ul>
}
}
else
{
<ul class="nav navbar-nav navbar-right">
<li>@Html.ActionLink("Register", "Register", "Home", routeValues: null, htmlAttributes: new { id = "registerLink" })</li>
<li>@Html.ActionLink("Log in", "Login", "Home", routeValues: null, htmlAttributes: new { id = "loginLink" })</li>
</ul>
}
[HttpPost]
public ActionResult Banner(Banner banner, HttpPostedFileBase file)
{
if (Session["AdminName"] != null)
{
if (ModelState.IsValid)
{
using (CarsStoreDbContext db = new CarsStoreDbContext())
{
if (file != null)
{
if (file.ContentLength > 0)
{
if (Path.GetExtension(file.FileName).ToLower() == ".jpg"
|| Path.GetExtension(file.FileName).ToLower() == ".png"
|| Path.GetExtension(file.FileName).ToLower() == ".gif"
|| Path.GetExtension(file.FileName).ToLower() == ".jpeg")
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/Content/img/employee"), file.FileName);
banner.imgFileName = file.FileName;
file.SaveAs(path);
}
}
}
db.banner.Add(banner);
db.SaveChanges();
}
ModelState.Clear();
}
return View();
}
else
{
return RedirectToAction("Index", "AdminAccount");
}
}