Link to home
Start Free TrialLog in
Avatar of champ_010
champ_010

asked on

Retreiving Session Variable

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'

Avatar of ihenry
ihenry

The error comes from this line
string cartItems=Session.["NumItems"];

remove the dot to
string cartItems=Session["NumItems"];
try changing this:
string cartItems=Session.["NumItems"];

to this:
string cartItems=Session["NumItems"];
Avatar of champ_010

ASKER

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'

???
Try ToString..
string cartItems=Session["NumItems"].ToString();
-Baan
Try

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

John

do you include this in the page?

<%@ Import Namespace="System.WebUI" %>
ASKER CERTIFIED SOLUTION
Avatar of ihenry
ihenry

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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'

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%>

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" %> .