I am trying to create a seesion variable in a class. When I try to compile it I get the following error.
CS0103: The name 'Session'does not exist in the class or namespace 'DatabaseConnection.DbConnect'
In the class file I try to create the session variable as follows
Session["CompanyName"]="Name";
Anyone have any ideas of my problem?HttpContext.Current.Session
When you use "Session" in a WebForm, you are accessing a property (Page.Session), which returns an HttpSessionState object, which you then index using ["CompanyName"]. If your class is not derived from Page (or several other classes which have a Session property), then your class doesn't have a Session property for you to use. Fortunately you can use the static method HttpContext.Current to get the current HttpContext object, which then gives you access to session state through its Session property.
AutoFed
Thanks for this.
How do I convert a session object to a boolean type or an integer?
You can use, Convert.ToInt32 and Convert.ToBoolean methods. These are the static methods of "Convert" class, which take object type as the parameter. So, you can pass your session variable to these methods to convert to the required type
It depends on what object you assigned to the session variable. If you assigned an integer to the session variable, e.g. Session["MyInt"] = 16, then you just need to cast it back to an int when you read it out, int myInt = (int)Session["myInt"]. If you assigned a string to the sesson variable, e.g. Session["MyInt"] = "16", and you want to convert that to an int, you can use Int32.Parse. You should only use the Convert functions if you don't know what the type of the object is, since there is some extra overhead in determining what the type is before converting it.
AutoFed
0 comments:
Post a Comment