• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 579
  • Last Modified:

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'

0
champ_010
Asked:
champ_010
1 Solution
 
ihenryCommented:
The error comes from this line
string cartItems=Session.["NumItems"];

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

to this:
string cartItems=Session["NumItems"];
0
 
champ_010Author Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
DotNetLover_BaanCommented:
Try ToString..
string cartItems=Session["NumItems"].ToString();
-Baan
0
 
jnhorstCommented:
Try

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

John
0
 
ihenryCommented:

do you include this in the page?

<%@ Import Namespace="System.WebUI" %>
0
 
ihenryCommented:
sorry, typo

<%@ Import Namespace="System.Web.UI" %>
0
 
champ_010Author Commented:
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
 
champ_010Author Commented:

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
 
champ_010Author Commented:

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now