简体   繁体   中英

I am getting Null exception error ASP.NET MVC

I am working on my semester end project.

Application was working fine, but when I added product function it worked once. Then I started getting null exception in the function. Now i am also getting same error in log in function as well.

I am getting this error:

enter image description here

enter image description here

This is my code:

[HttpPost]
public ActionResult LogIn(User ur, string ReturnUrl)
{
    if (isValid(ur) == true)
    {
        FormsAuthentication.SetAuthCookie(ur.UserName, false);
        Session["Usrname"] = ur.UserName;

        if (ReturnUrl != null)
        {
            return Redirect(ReturnUrl);
        }
        else
        {
            return RedirectToAction("Index", "Dashboard");
        }
    }
    else
    {
        ViewBag.msg = "Email Or Password is incorrect";
        return View();
    }
}

bool isValid(User ur)
{
    var cred = u_DB.Users.Where(model => model.UserEmail == ur.UserEmail && model.UserPassword == ur.UserPassword).FirstOrDefault();

    if (cred != null)
    {
       return true; 
    }
    else
    {
       return false;
    }
}

Try using Refresh method.

try{
      DB.SaveChanges();
   }
catch(OptimisticConcurrencyException){
   DB.Refresh(RefreshMode.ClientWins, yourentity);
   DB.SaveChanges();
}

And for the NullException Error in this Image, try the below code line.

在此处输入图像描述

List<SubCategory> Scat = P_DB.subCategories!.toList();

Hope this helps. If it works let me know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM