2

I was getting data from the database in this controller and then I am storing the username into a session it works when I move to the view page after login.

public ActionResult shoppage(assign s)
{

    if (ModelState.IsValid)
    {
        using (transactionEntities db = new transactionEntities())
        {
            var obj = db.assigns.Where(a => a.Username.Equals(s.Username) && a.Password.Equals(s.Password)).FirstOrDefault();
            if (obj != null)
            {
                Session["Username"] = s.Username.ToString();

                return View();
            }
            else
            {
                return RedirectToAction("login");
            }
        }

iwa getting username there in Dashboard view

But when I try to access it in another action result it gives me a null value exception

public ActionResult insertwatch()
{
    if (Session["Username"] != null)
    {
        name = Session["Username"].ToString();
    }

    if (Request.Form["submit1"] != null)
    {
        transactionEntities dt = new transactionEntities();
        itemthing it = new itemthing();
        it.uname = name.ToString();
        it.itemprice = "1200";
        it.itemname = "lucia";
        dt.itemthings.Add(it);
        return RedirectToAction("shoppage");
    }
    else if (Request.Form["submit2"] != null)
    {
        transactionEntities dt = new transactionEntities();
        itemthing it = new itemthing();
        it.uname = name.ToString();
        it.itemprice = "1800";
        it.itemname = "Benson";
        dt.itemthings.Add(it);
        return RedirectToAction("shoppage");
    }

Error: this was the error

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
yaseen
  • 43
  • 4
  • 2
    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 Jun 26 '21 at 06:12
  • I am not able to understand your question. Are you trying to assign a new value to the session variable which is already assigned ? – neehu Jun 26 '21 at 07:24

1 Answers1

1

You may need to add a null check the property:-

 public ActionResult shoppage(assign s)
    {

        if (ModelState.IsValid)
        {
            using (transactionEntities db = new transactionEntities())
            {
                var obj = db.assigns.Where(a => a.Username.Equals(s.Username) && a.Password.Equals(s.Password)).FirstOrDefault();
                if (obj != null)
                {
                     if (HttpContext.Current.Session["Username"] == null)  //add this code
                          return string.Empty;
                    else
                        //Session["Username"] = s.Username.ToString();
                        HttpContext.Current.Session["Username"] = s.Username.ToString();

                    return View();
                }
                else
                {
                    return RedirectToAction("login");
                }
            }

Or you can simply use this line of code:-


//clarify code

if (Session["Username"]!=null)
{
   Session["Username"] = s.Username.ToString();
   return View();
}

//clarify code

Or you can use something like that of the set session and get the store value of the session:-

Set the Session

string user = s.Username.ToString();   
HttpContext.Session.SetString("UserToken", user);

Get the stored value from the session:-

var token = HttpContext.Session.GetString("UserToken");

I think it will resolve your issue.

Pritom Sarkar
  • 2,154
  • 3
  • 11
  • 26