0

I have a following View Method in an ASP.NET MVC 3 Controller that retrieves data from Amazon SimpleDb, stores it in a list and then stores that list object in a session. But at the line where I am storing the userBox object in a session (Session["userBox"] = userBox), I am getting a NullReferenceException. I am sure that userBox is not null. Even if I try to store a simple string in a session (like Session["userBox"] = "test") I still get NullReferenceException.

Here is the code:

  public ActionResult SetSidebarAccountBoxSessions(string id)
    {
        string selectExpression = "select * from MySimpleDBDomain where itemName()='" + id + "'";

        SelectRequest sreq = new SelectRequest().WithSelectExpression(selectExpression);

        SelectResponse sres = sdb.Select(sreq);

        List<User> userBox = new List<User>();



        if (sres.IsSetSelectResult())
        {
            SelectResult selectresult = sres.SelectResult;

            foreach (Item item in selectresult.Item)
            {

                string a = item.Name;


                userBox.Add(new User
                {


                    imageThug = item.Attribute[0].Value,
                    name = item.Attribute[3].Value,
                    bio = item.Attribute[1].Value



                });

            }
        }

        Session["userBox"] = userBox;

        return View();


    }

I am calling this SetSideBarAccountBoxSessions(id) method from another controller method:

 HomeController hc = new HomeController();
hc.SetSidebarAccountBoxSessions(item.Name);

Can this be the problem? Please help.

Umair Khan Jadoon
  • 2,874
  • 11
  • 42
  • 63
  • possible duplicate of [What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) – John Saunders Oct 02 '11 at 14:57
  • If you check `Session` just before the `Session["userBox"] = userBox;` line, is it `null`? – Jeff Ogata Oct 02 '11 at 17:11

1 Answers1

0

I think this problem is related to the fact that you create HomeController by yourself. You can try to use TransferToRouteResult to transfer the action to HomeController.

You an find the code of TransferToRouteResult in this link:

How to simulate Server.Transfer in ASP.NET MVC?

Community
  • 1
  • 1
Massimo Zerbini
  • 3,125
  • 22
  • 22