Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi Team

I am having a problem, i am saving a value column to my database table. When it reaches to the method to save that value it throws an exception as being null. How do i resolve this issue?

What I have tried:

// Model
public class eNtsaCourses
  {
      [Key]
      public Guid? Id { get; set; }
      public string Course { get; set; }
      public string Nickname { get; set; }
      public string Term { get; set; }
      public string EnrolledAs { get; set; }
      public bool Published { get; set; }
  }

//GET://Courses/Courses-All.
    [Route("Home/Courses")]
  public ActionResult Courses(RegCoursesViewModel eNtsaCourses)
    {
        if(ModelState.IsValid)
        {
            cb.eNtsaCourse.Add(eNtsaCourses.Courses);
            cb.SaveChanges(); // This gets thrown here.
        }
        return View(eNtsaCourses);
    }

[HttpPost]
      public JsonResult AddCoursesRegistration(string CourseName, string clientNo)
      {
          return Json(true, JsonRequestBehavior.AllowGet);
      }
Posted
Updated 31-Aug-20 2:58am
Comments
Richard MacCutchan 31-Aug-20 8:13am    
As we have said many times, the only way to diagnose issues like this is by using the debugger to find out which item is null.
gcogco10 31-Aug-20 8:19am    
@Richard the null is thrown cb.eNtsaCourse.Add(eNtsaCourses.Courses);
F-ES Sitecore 31-Aug-20 9:47am    
The problem is likely to be with the view such that it isn't passing a RegCoursesViewModel.Courses object to the action.

1 solution

If you say the error is at:
Quote:
cb.eNtsaCourse.Add(eNtsaCourses.Courses);

eNtsaCourses.Courses seems to be the culprit. Mostly, eNtsaCourses is null.
OR
eNtsaCourses.Courses is null.

While trying to save the value null for Courses, it throws an error. (Error at DTO/Entity level)

You need to:
1. make sure that the request holds the value if it is supposed to
2. Irrespective of request, your code should handle validations around input to avoid such errors.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900