Solved

Retreiving Session Variable

Posted on 2004-09-25
10
576 Views
Last Modified: 2012-06-22
Hi,

I made a Session Variable on one page and would like to display it in my .ascx file so that it shows up on a bunch of different pages.  I don't know if I'm retreiving the variable the wrong way or if I'm missing something to allow it to work in an .ascx file.

//-----Sessions created:

      Session["Sbtl"]=Subtotal;
      Session["NumItems"]=tblCart.Rows.Count;

//------My .ascx file:

<script language="C#" runat="server">
     string cartItems=Session.["NumItems"];
     string cartSub=Session["Sbtl"];  
</script>

<table>
  <tr>
    <td>
        <asp:Label ID="Items" Text="<%=cartItems%>" runat="server"/>
        <asp:Label ID="Subtotal" Text="<%=cartSub%>" runat="server"/>
    </td>
  </tr>
</table>

//------ Error Message:

An object reference is required for the nonstatic field, method, or property 'System.Web.UI.UserControl.Session'

0
Comment
Question by:champ_010
[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
10 Comments
 
LVL 20

Expert Comment

by:ihenry
ID: 12149767
The error comes from this line
string cartItems=Session.["NumItems"];

remove the dot to
string cartItems=Session["NumItems"];
0
 
LVL 3

Expert Comment

by:nhewitt21
ID: 12149768
try changing this:
string cartItems=Session.["NumItems"];

to this:
string cartItems=Session["NumItems"];
0
 
LVL 1

Author Comment

by:champ_010
ID: 12151376
Hi , thanks--no dot but the same error:

An object reference is required for the nonstatic field, method, or property 'System.Web.UI.UserControl.Session'

???
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12151448
Try ToString..
string cartItems=Session["NumItems"].ToString();
-Baan
0
 
LVL 10

Expert Comment

by:jnhorst
ID: 12151472
Try

string cartItems = this.Page.Session["NumItems"];

John
0
 
LVL 20

Expert Comment

by:ihenry
ID: 12151528

do you include this in the page?

<%@ Import Namespace="System.WebUI" %>
0
 
LVL 20

Accepted Solution

by:
ihenry earned 100 total points
ID: 12151534
sorry, typo

<%@ Import Namespace="System.Web.UI" %>
0
 
LVL 1

Author Comment

by:champ_010
ID: 12152239
Yikes, none of the above.

On the page that the session was created I've printed it out and it looks exactly the way I want it to:

      Session["Sbtl"]=Subtotal;
      Session["NumItems"]=tblCart.Rows.Count;
      
      decimal Sub=(Decimal)Session["Sbtl"];
      int NI=(Int32)Session["NumItems"];
      
      Response.Write(Convert.ToString(String.Format("{0:c}",Sub)));
      Response.Write(NI);

But as soon as I try to get it on another page whether it's aspx or ascx:

//----- In the code:

decimal Sub=(Decimal)Session["Sbtl"];
int NI=(Int32)Session["NumItems"];

//---In the HTML:

<%=Sub%>
<%=NI%>

I still get the same error:

An object reference is required for the nonstatic field, method, or property 'System.Web.UI.UserControl.Session'
0
 
LVL 1

Author Comment

by:champ_010
ID: 12152336

Alright, alright I got it. Does it show I'm a newbie or what--I wasn't sure if all that Page_Load stuff applied to an ascx page or not but I tried it out and that's what my problem was:

<%@ Import Namespace="System.Web.UI.WebControls"%>
<script language="C#" runat="server">
string Sub;
decimal SSB;
int NI;

void Page_Load()
{
     if((Session["Sbtl"]==null) && (Session["NI"]==null))
     {
      decSB=0.00M;
      Sub=Convert.ToString(String.Format("{0:c}",SSB));
      NI=0;
     }else{
      decSB=(Decimal)Session["Sbtl"];
      Sub=Convert.ToString(String.Format("{0:c}",SSB));
      NI=(Int32)Session["NumItems"];
    }
}
</script>

Items: <%=NI%>  Subtotal: <%=Sub%>
0
 
LVL 1

Author Comment

by:champ_010
ID: 12152338

Thanks for everyone's help but since I have to pick an answer I chose ihenry's because I did forget the <%@ Import Namespace="System.Web.UI" %> .
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

695 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