Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5979283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:38:15+00:00 2026-05-22T21:38:15+00:00

My question is one line yet this is very confusing me. Why i can

  • 0

My question is one line yet this is very confusing me.
Why i can not declare and initialize a session variable in partial class of a page it throws an error saying

Error 1 Invalid token ‘[‘ in class,
struct, or interface member
declaration E:\ASP.NET\Trial\statemanagement.aspx.cs 17 12 E:\ASP.NET\Trial\

below is the code i tried

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class statemanagement : System.Web.UI.Page
{
    int count=0;
    Session["FirstName"] = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
     if(!IsPostBack)
     {
         count = int.Parse(Session["FirstName"].ToString());
     }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        Response.Write(count.ToString());
        Session["FirstName"] =++count;

    }
}

i dont get the error for count variable i dont know why?

Any help will be regarded thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:38:16+00:00Added an answer on May 22, 2026 at 9:38 pm

    That is not a declaration. It is an assignment. You can not place an assignment statement directly inside a class. You have to place it inside a method or property.

    Refering to ASP.NET Session State Overview

    Session variables are stored in a
    SessionStateItemCollection object that
    is exposed through the
    HttpContext.Session property. In an
    ASP.NET page, the current session
    variables are exposed through the
    Session property of the Page object.

    The collection of session variables is
    indexed by the name of the variable or
    by an integer index. Session variables
    are created by referring to the
    session variable by name. You do not
    have to declare a session variable or
    explicitly add it to the collection.

    If you like to initialize the session variables independently from the page you can use application event handlers such as Application_Start and Session_Start which you can find in the global.asax file .

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Simple question: How do I do this on one line: my $foo = $bar->{baz};
Here is one more newbie question: require 'tasks/rails' I saw this line in Rakefile
This may not be the kind of question one should ask on StackOverflow, but
I have a simple question related to one-line programming. First an example: function test(a)
In the How Can I Expose Only a Fragment of IList<> question one of
One question about protected constructor. I learnt that the protected constructor can be used
Yet another IE is doing something different from other browsers question but this is
Yes, this is probably yet another greatest-n-per-group question... But I've tried at least a
I apologise in advance for this confusing question, wall of text, and awful trigger.
I'm sure this question has been asked before, but I can't seem to find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.