[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

List object in session C# .NET 3.5

Posted on 2010-09-03
4
Medium Priority
?
986 Views
Last Modified: 2013-11-08
I'm new to .NET but not to web app development.  I'm going to ask a question about how I am trying to do something.  If you could, please help me answer that question - even if there is a better way to do what I'm doing.  It will accomplish what I need and help to educate me.

Then if there's a better way, I'm all ears :)  But as I'm still learning, even learning it the non-optimal way will benefit me.

It's a rather simple problem.  I'm using MVC and in my controllers I want to be able to add feedback messages to an object in Session.  Then I have a shared view which I'm including in the views I want to render the messages to the user.  So the flow is like this:

- In global, at startup, I create a new List<string> object and put it in Session:
List<string> messageList = new List<string>();
Session["messages"] = messageList;

- Run the site
- Call to controller
- Controller does its thing (say... delete a database record)
- Controller puts "X Deleted Successfully" into a List<string> object in session:
((List<string>)Session["messages"]).Add("You succesfully deleted record " + id);

- Controller redirects to view
- View includes the shared MessageProcessor view (control)
- MessageProcessor tries to grab the list object from session, check if there are messages, and if yes... render them.
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
<%
    // Get the messages array from the session object.
    List<string> messageList = (List<string>)Session["messages"];
    if (messageList.Count() > 0)
    {
        // Process messages by displaying them
        foreach(var message in messageList)
        {
            Response.Write("<li>" + message);
        }
    }
 %>      
</asp:Content>

I'm getting the following exception:

Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: source

Source Error:

Line 5:      // Get the messages array from the session object.
Line 6:      List<string> messageList = (List<string>)Session["messages"];
Line 7:      if (messageList.Count() > 0)
Line 8:      {
Line 9:          // Process messages by displaying them

Which says to me, the object is not coming out of session correctly.  Is my Line 6 code to pull out the object from session fubared in some subtle way?

Thanks for any help!
// In global:

List<string> messageList = new List<string>();
Session["messages"] = messageList;

// In controller:
((List<string>)Session["messages"]).Add("You succesfully deleted record " + id);

// In control (shared view)
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
<%
    // Get the messages array from the session object.
    List<string> messageList = (List<string>)Session["messages"];
    if (messageList.Count() > 0)
    {
        // Process messages by displaying them
        foreach(var message in messageList)
        {
            Response.Write("<li>" + message);
        }
    }
 %>	
</asp:Content>

Open in new window

0
Comment
Question by:trippy1976
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1000 total points
ID: 33603094
My usual approach, with the Session, is to have a List<string> reference, and modify that, and then store the entire list in the Session, so I am not sure if you can modify the contents of a List within a Session variable the way that you are trying to use the Add method call to a casted List<string> reference.
0
 
LVL 6

Expert Comment

by:aymoon80
ID: 33606231
you can use the debugger to get what is stored in session object also followup the calling of your code in the session startup.
List<string> messageList = new List<string>();
Session["messages"] = messageList;

you should close the browser after every trial to remove the session
0
 
LVL 6

Assisted Solution

by:aymoon80
aymoon80 earned 1000 total points
ID: 33606236
Because I understood that you are new to C#
to use the debugger first put a break point on the line you want to start from

in your case the following line:
List<string> messageList = new List<string>();

press F9 to put break point

press F5 to start debugger
press F10 to execute next line of code till you got the exception
0
 
LVL 4

Author Closing Comment

by:trippy1976
ID: 33613408
I know how to use a debugger ;)

Anyway, what turned out to be the issue was I tried to initialize the session variable in the Application_Start() method.  At this point there is apparently no session.  You can add a Session_Start() method into the global file and initialize any session variables there instead and *then* it all works dandy.  Thanks for the help everyone.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question