trippy1976
asked on
List object in session C# .NET 3.5
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["me ssages"]). 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="Titl eContent" runat="server">
<%
// Get the messages array from the session object.
List<string> messageList = (List<string>)Session["mes sages"];
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.ArgumentNullExcepti on: 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["mes sages"];
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!
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["me
- 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="Titl
<%
// Get the messages array from the session object.
List<string> messageList = (List<string>)Session["mes
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.ArgumentNullExcepti
Parameter name: source
Source Error:
Line 5: // Get the messages array from the session object.
Line 6: List<string> messageList = (List<string>)Session["mes
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>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
List<string> messageList = new List<string>();
Session["messages"] = messageList;
you should close the browser after every trial to remove the session