Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi frnds ,
i have declares a static variable in a class and i need to assign session value to that static variable like this below . .
C#
public partial class Home_sales : System.Web.UI.Page
{
static string id = Session["id"].ToString();
}


but i am getting this compilation error
C#
Error	26	An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Session.get'	


please tell me guys how to assign a session value to static variable . .. please help me

thanks
mani. .
Posted

1 solution

yes you can assign value to static variable from non static function. you can not assign session value in this manner, you have to create function for that like this... :)

C#
public partial class ShipmentOverview : System.Web.UI.Page
    {
        public static string SessionData = string.Empty;

        public void SetSession() {

            
            SessionData = Session["id"].ToString();
        
        }

}

In reverse you can not assign non static variable to static function directly.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900