Solved

Retreiving Session Variable

Posted on 2004-09-25
10
570 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
Comment Utility
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
Comment Utility
try changing this:
string cartItems=Session.["NumItems"];

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

Author Comment

by:champ_010
Comment Utility
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
Comment Utility
Try ToString..
string cartItems=Session["NumItems"].ToString();
-Baan
0
 
LVL 10

Expert Comment

by:jnhorst
Comment Utility
Try

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

John
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 20

Expert Comment

by:ihenry
Comment Utility

do you include this in the page?

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

Accepted Solution

by:
ihenry earned 100 total points
Comment Utility
sorry, typo

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

Author Comment

by:champ_010
Comment Utility
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
Comment Utility

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
Comment Utility

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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