-1

I got question on this error which I cannot solve. I am using Entity Framework database first. And before that I have deleted the entity model and create a new one. I am not sure it will affect or not.

This is my viewModel

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;

namespace MVCTutorial.Models
{
public class OtherIncViewModel
{
    public virtual DbSet<User> Users { get; set; }
    public virtual DbSet<UserGroup> UserGroups { get; set; }
    public virtual DbSet<Patient> Patients { get; set; }

    //User
    public int UserId { get; set; }
    public string UserFullName { get; set; }
    public string UserIC { get; set; }
    [DataType(DataType.Password)]
    [Required(ErrorMessage = "This field is requiered.")]
    public string UserPassword { get; set; }
    public string UserEmail { get; set; }
    public Nullable<int> UserGroupId { get; set; }
    public string UserMMCRegistrationNo { get; set; }
    [DisplayName("UserName")]
    [Required(ErrorMessage = "This field is requiered.")]
    public string UserLogin { get; set; }
    public string UserFaxNo { get; set; }
    public string UserDesignation { get; set; }
    public string UserStatus { get; set; }
    public string UserContact { get; set; }
    public virtual UserGroup UserGroup { get; set; }
    public string LoginErrorMessage { get; set; }

    //Patient
    public int PatientId { get; set; }
    public string PatientName { get; set; }
    public string PatientIC { get; set; }
    public int PatientAge { get; set; }
    public string PatientGender { get; set; }
    public string Hospital { get; set; }
    public string Ward { get; set; }
    public string Department { get; set; }
    public string Barcode { get; set; }
    public string Race { get; set; }
    public string PatientErrorMessage { get; set; }
    public virtual ICollection<CaseOtherInc> CaseOtherIncs { get; set; }
    public virtual ICollection<DraftCaseOtherInc> DraftCaseOtherIncs { get; set; }

}
}

This is my controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCTutorial.Models;


namespace MVCTutorial.Controllers
{
public class WardStaffController : Controller
{
    OtherIncContext db = new OtherIncContext();
    // GET: WardStaff
    public ActionResult Index() //Ward Staff Inbox
    {
        return View();
    }

    public ActionResult CreateTransfusion() //Ward Staff Create Transfusion
    {
        return View();
    }

    public ActionResult CreateIBCT() //Ward Staff Create IBCT
    {
        return View();
    }

    public ActionResult CreateNearMiss() //Ward Staff Create Near Miss
    {
        return View();
    }

    [HttpPost] 
    public ActionResult SearchPatient(MVCTutorial.Models.OtherIncViewModel patient)
    {

        using (OtherIncContext db = new OtherIncContext())
        { //I am wondering here why my local variable does not reference the namespace in this case is MVCTutorial. Is this why i get the null exception? I have check my database...i didn't have null record.
            var patientDetails = db.Patients.Where(x => x.PatientIC == patient.PatientIC).FirstOrDefault(); 

            if (patientDetails == null)
            {
                patient.PatientErrorMessage = "Please enter patient's IC number.";
                return View("CreateOtherIncident", patient);
            }
            else
            {

                Session["PatientName"] = patientDetails.PatientName;
            }
            return RedirectToAction("CreateOtherIncident", "WardStaff");
        }

    }

This is my view

@model MVCTutorial.Models.OtherIncViewModel
@{
ViewBag.Title = "CreateOtherIncident";
Layout = "~/Views/Shared/_LayoutWardStaff.cshtml";
}

<h2>CreateOtherIncident</h2>

<h2>Reported By:</h2>

<p> Name: @Session["UserFullName"].ToString()</p>
<p>Email Address: @Session["UserEmail"].ToString()</p>
<p>Date:</p>
<p>Designation No.: @Session["UserDesignation"].ToString()</p>
<p>Tel.No: @Session["UserContact"].ToString()</p>
<p>Fax.No: @Session["UserFaxNo"].ToString()</p>

<h2>Patient Details:</h2>

@Html.LabelFor(model=>model.PatientIC)
@Html.EditorFor(model=>model.PatientIC)

<p>Patient Name: @Session["PatientName"].ToString()</p> //I get the exception here.

I suspect it is because of the local variable, but I cannot find any further error or exception on that.

halfer
  • 19,824
  • 17
  • 99
  • 186
ellie
  • 3
  • 2
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) –  Feb 15 '18 at 10:50
  • 1
    Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Feb 15 '18 at 11:42

1 Answers1

0

Before the exception, To check session should not be null.

like:

@if (HttpContext.Current.Session["PatientName"] != null)
            { //@Session["PatientName"].ToString()
}
Niteen
  • 121
  • 13