Solved

Retreiving Session Variable

Posted on 2004-09-25
10
571 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now