0

Hello guys I am making a college website project in MVC it has admission form so whenever a admission form is filled by user and gets submitted it goes for approval and the user receives his admission code to check the status of his/her account approval so I am having a problem here.

The Code:

Model:

  public partial class admission_tbl
{
    public int adm_id { get; set; } //- by this user checks his status//
    [Required]
    [StringLength(50)]
    public string std_name { get; set; }
    [Required]
    [StringLength(50)]
    public string std_father { get; set; }
    [Required]
    [StringLength(50)]
    public string std_mother { get; set; }
    [AgeRangeValidation(ErrorMessage = "Age must be between 18 - 28", MinAge = 18, MaxAge = 28)]
    [Required]
    [DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]

    public System.DateTime DOB { get; set; }
    [Required]
    public string std_gender { get; set; }
    [Required]
    public string R_address { get; set; }
    [Required]
    public string P_address { get; set; }
    [Required]
    public string adm_for { get; set; }
    [Required]
    public string university { get; set; }
    [Required]
    public string E_no { get; set; }
    [Required]
    public string Center { get; set; }
    [Required]
    public string City { get; set; }
    [Required]
    public string Field { get; set; }
    [Required]
    public string Marks_secured { get; set; }
    [Required]
    public string out_of { get; set; }
    [Required]
    public string Class_obtained { get; set; }

    public string Sports_details { get; set; }
    public string adm_status { get; set; } //--This is the method which i am using to control account 
                                                approval or non approval status.
           }}

Controller:

 public ActionResult Status_check()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Status_check(admission_tbl svm)
    {
        admission_tbl u = db.admission_tbl.Where(a => a.adm_id == svm.adm_id).SingleOrDefault();


        if (u.adm_status == "Approved")
        {
            ViewBag.Message1 = "Your account is Approved";
        }
        else if (u.adm_status == "Unapproved")
        {
            ViewBag.Message = "Your account is not Approved ";

        }
        else
        {
            ViewBag.Message = "Your account is in pending please wait";
        }

        return View();
    }

So, here in status check the user inputs his admission code to check whether account is approved or not everything works fine but the problem is when I try to input a random number input it throws null exception error instead I want to throw a message error for example "Invalid Admission Code no such exists" i couldn't catch it what am i missing?

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
Mac
  • 17
  • 4
  • Does this answer your question? [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) – Yong Shun Apr 17 '21 at 00:15
  • Yes I checked up on this still didn't found any solution. – Mac Apr 17 '21 at 00:22

1 Answers1

1

For the scenario that svm.adm_id is not existed in the records for admission_tbl

admission_tbl u = db.admission_tbl.SingleOrDefault(a => a.adm_id == svm.adm_id);

This .SingleOrDefault() will return NULL. Thus when you try to access adm_status from u which is null, you will get NullReferenceException.

To solve this issue, you need to add validation to check the u object cannot be null before proceeding to access its inner properties.

[HttpPost]
public ActionResult Status_check(admission_tbl svm)
{
    admission_tbl u = db.admission_tbl.Where(a => a.adm_id == svm.adm_id).SingleOrDefault();

    // Validate u cannot be null, if null do error handling
    if (u == null)
    {
        ViewBag.Message = "Invalid Admission Code no such exists";
        return View();
    }

    if (u.adm_status == "Approved")
    {
        ViewBag.Message1 = "Your account is Approved";
    }
    else if (u.adm_status == "Unapproved")
    {
        ViewBag.Message = "Your account is not Approved ";

    }
    else
    {
        ViewBag.Message = "Your account is in pending please wait";
    }

    return View();
}

Note: This statement

db.admission_tbl.Where(a => a.adm_id == svm.adm_id).SingleOrDefault();

can be replaced to:

db.admission_tbl.SingleOrDefault(a => a.adm_id == svm.adm_id);

Both will return same result and return null if the record is not found.

Second Edit version: You can bind your error message in ViewBag, or add any handling for the scenario and immediately exit

ViewBag.Message = "Invalid Admission Code no such exists";
return View();
Yong Shun
  • 960
  • 8
  • 12
  • Thanks for the detailed explanation @yong but is it possible to throw just a error message on the same page instead of throwing exception? – Mac Apr 17 '21 at 00:32
  • Hi @Mac, yes sure, just replace the `throw new Exception()` with `ViewBag.Message = "Invalid Admission Code no such exists"`. I updated the code. You may refer it. Thanks. – Yong Shun Apr 17 '21 at 00:34
  • 1
    Yes this is my answer ! Thanks again @yong – Mac Apr 17 '21 at 00:44