Should I save changing of WebForm variables to Session variables?

Global.asax.cs
protected void Session_Start(Object sender, EventArgs e)
{
    int a=0;
    Session["a"]=a;
}
WebForm1.aspx.cs
public class WebForm1 : System.Web.UI.Page
{
    public int a;
    private void Page_Load(object sender, System.EventArgs e)
    {
        a=(int)Session["a"];
    }
    private void Button1_Click(object sender, System.EventArgs e)
    {
        a=a+1; //should I save this changing to Sassion["a"] or It saves automaticaly?
} }
0
x4444
6/30/2005 11:03:34 AM
asp.net.getting-started 91979 articles. 3 followers. Follow

3 Replies
619 Views

Similar Articles

[PageSpeed] 59

Now, someone may need to double-check this, but the cast to int should
make a copy of the variable in method scope, so therefore it wouldn't
update the session variable.

try it out and see.... I'll check the docs shortly


RTFM - straight talk for web developers. Unmoderated, uncensored, occasionally unreadable

Jason Brown - MVP, IIS
0
Atrax
6/30/2005 1:00:32 PM
 Atrax wrote:
Now, someone may need to double-check this, but the cast to int should make a copy of the variable in method scope, so therefore it wouldn't update the session variable.

try it out and see.... I'll check the docs shortly

It isn't specifically the cast to int that makes a copy of the value, rather the assigning of the value and not the reference to the address in memory (i.e. pointers).

And, just to confirm, the Session variable wouldn't be updated.


Steven Bey

Recursion: see Recursion
0
stevenbey
6/30/2005 1:39:17 PM
 x4444 wrote:
Global.asax.cs
protected void Session_Start(Object sender, EventArgs e)
{
    int a=0;
    Session["a"]=a;
}
WebForm1.aspx.cs
public class WebForm1 : System.Web.UI.Page
{
    public int a;
    private void Page_Load(object sender, System.EventArgs e)
    {
        a=(int)Session["a"];
    }
    private void Button1_Click(object sender, System.EventArgs e)
    {
        a=a+1; //should I save this changing to Sassion["a"] or It saves automaticaly?
} }

This isn't meant as a criticism but it seems, to me at least, rather unnecessary to create variables when you don't need to. For example:

Global.asax.cs
protected void Session_Start(Object sender, EventArgs e)
{
    Session["a"]=0;
}
WebForm1.aspx.cs
public class WebForm1 : System.Web.UI.Page
{
    private void Button1_Click(object sender, System.EventArgs e)
    {
        ((int) Session["a"])++;
} }



























Steven Bey

Recursion: see Recursion
0
stevenbey
6/30/2005 1:45:07 PM
Reply:

Similar Artilces:

After a looong night managed to change Session mode from inProc to SQLServer but still can't get the session variables to be saved
Hi,  I ran my Shopping Cart project and somehow everything worked fine(while in inProc), session variables were saved fine. I made some changes to my project and since then keep getting null session variables, no matter what I do. It took me the whole night to figure out how to turn to SQLServer mode,  marked custom classes as 'Serializable' with [Serializable] before the class definition but everything's in vain. Getting the same damned 'nulls' instead of the saved objects. private void Page_Load(object sender, System.EventArgs e)         {...

How to get session variable value into a static variable
Hi, On page preinit i am adding few session variables. Now i want to retrive these session variable values into my static variables. protected void Page_PreInit(object sender, EventArgs e) {Session.Add("T", "Default"); Session.Add("T1", "Red"); Session.Add("T2", "Blue");   }   i have few static variables which shud contain these vaules.  public string TT; public string TT1; public string TextForeColor =sess = "#FFFFFF";   i want to get T value into TT ... T1 into TT1..and so on &n...

session variables getting changes
Hi,   I am having 2 frames in my page top_frameand middle_frame, the top frame has got a dropdown list, now i am calling a function in the selectedindexchanged to assign the text and value to two session variables when the user changes his selection in the dropdown list, now i also have an anchor tag close to the dropdownlist, when the user changes the selection in the dropdownlist the two session variables will be initialised and then the user clicks the anchor link and on the basis of the session variables the middle_frame page will  get changed..till this it is working properly...

My session variables get cleared session is alive when code is changed in the class files.
Dear Friends,I am working on visual studio 2005. I have written some forms which are using some classes. I keep on making changes in form code. Session variables retain their values. But when a minor change is done in the class file, session variables are cleared of where as the session is alive.Please help me in troubleshooting this.Thanks in advance.Best Regards,Fazal.  You are saying right. Store static values during development like    page_load         Session("variable")=value  check some articleshttp://www.co...

variable not a variable
Error:Must declare the variable '@cartDateTime'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Data.SqlClient.SqlException: Must declare the variable '@cartDateTime'. Dim connStr As SqlConnection            Dim cmdInsert As SqlCommand            Dim strInsert As String      &n...

variable of variables
Say I have 2 scenarios and I want to use a different variable depending on which scenario applies without coding this logic. So I could have a database table t_variables which has: Scenario: Variable: --------- -------- 1 v_name 2 v_company In my Powerbuilder code I'd have values in v_name & v_company. What I'd like to be able to do is to select which variable to use from the table and assign that to something, e.g. select variable into v_variable from t_variables where scenario = 1 so at this point v_variable would be 'v_name'...

variable variables?
Hello! How to make a variable receive an increment in the name? I want to do it for example: I have: Label1->Caption = "1"; Label2->Caption = "2"; Label3->Caption = "3"; Label4->Caption = "4"; And I want to do something like that: for (i=1; i<5; i++) { Label+i->Caption = i; } Any idea? Can I do something like that? Thanks and regards Alexandre Create a vector of pointers to the various objects. In your case below, construct a vector (called Labels) of pointers to TLable long enough to hold pointers to...

Variable Variables
Hi, Maybe the solution is very simple, but how do I create variable variables in vb.net? I want to use the value of a variable as a part of the name of another variable. A very simple example below: code: Dim a As String a = "1" Label a.Text = "hi" page: <asp:Label id="Label1" runat="server">Label</asp:Label> In need this to change a range of values by using just one loop. I can´t do that without creating a variable variable name. Hi, check out this little demo application I created for you: ...

shared variable or session variable???
I've asked this question on a couple of different forums and usenet and noone has offered a reply, so I thought I'd try it here... I have 2 different web apps that happen to do almost exactly the same things. One I did many months ago and the other is very recent. In both cases, I had data that needed to be available throughout all the functions of the page. Looking at the code, I noticed that in the older one I used a shared variable (woud be static in c#) to hold the needed data. In the newer app, I simply used a session variable. The data itself is a dataTable. My question is:...

Please help: Saving string variables in session variables, problems with national Æ, Ø or Å
When saving string variables in session variables and later on retrieve them, national characters causes trouble.For example danish / norwegian Ø will be convertet to: &#216;Somebody told me to change web.config globalization setting, like this:<globalizationfileEncoding="utf-8"requestEncoding="utf-8"responseEncoding="utf-8"culture="da-DK"uiCulture="da-DK"/>Unfortunately the problem is still thereThis is asp.net 2.0 beta 2.Regards Petter (Norway)regardsPetter Looks like it is doing a Server.htmlencode, just have to reverse it when you pull the session var. I think the article bel...

getting dropdown list value from usercontrol to change session variable
I have a problem, I have a user control that has 2 drop down lists, what I need to do is use the value of the selected item in the dropdown list to use in the selection of data in a datagrid on the main page. Basically I just need to take the values, and be able to plug them in (during a page load event) and use them in the query. Right now the usercontrol gets fired AFTER all of the other things are loaded. I thought that by setting a session to the value of the drpdown list would work, but its not triggering anything because 1, its a post back event, and two the usercontrol is...

Cache Variable vs. Session Variable
I'm anxious to get some feedback with regard to using a cache variable in lieu of a session variable. My research tells me that caching is the way to go for maintaining state on datasets. However, I've only seen the cache variable discussed as an alternative to an application variable (as opposed to the session variable). I'm actually exploring the idea using of using a cache variable as an alternative to a session variable for maintaining state on a dataset specific to each user (Each user is able to select the fields they wish to view; the user will then be able to page and sort on th...

getting the exported variable into a perl variable
------=_Part_110760_21946666.1155278901075 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi, I got a program which will access a exported variable in the shell. Can anyone help me with this? ------=_Part_110760_21946666.1155278901075-- Perl maintains environment variables in a special hash named %ENV Krishnakumar K.P Sr.Software Engineer Cybage Software Pvt. Ltd (An SEI-CMMI Level 5 Company) West Avenue, Kalyani Nagar Pune - 411 006 Tel: 020- 66041700/ 66044700 extn: 3178 Email: krishnakumarp@cyb...

How to read a variable stored in session variable?
I am assigning to a session variable more than one variable, and then I want to read them, but I don’ t know how to do it. For example: In my first page I store this variable in session variable object, thus: Dim email As String email = CmdDataReturn.Parameters("@e_mail").Value Session("user_menu") = email Then, in another page, I store in the same session variable another variable, thus: Dim offer_number As Int64 Offer_number = CmdDataReturn.Parameters("@Offer_id").Value Session("user...

Web resources about - Should I save changing of WebForm variables to Session variables? - asp.net.getting-started

Variable - Wikipedia, the free encyclopedia
Text is available under the Creative Commons Attribution-ShareAlike License ;additional terms may apply. By using this site, you agree to the ...

Westpac interest rate rise is one of many variable loan rate rises
WESTPAC is not alone in hitting its owner-occupier customers with a surprise interest rate rise, as dozens of other variable rate home loans ...

Airbnb upgrades variable pricing tool for hosts
Online home rental group Airbnb is rolling out an upgraded variable pricing tool that automatically raises or cuts room rates based on supply ...

Commonwealth Bank raises variable home loan rates
Home owners' mortgage costs are set to rise by hundreds of dollars a year, after the Commonwealth Bank became the latest bank to hike interest ...

CBA lifts variable home loan rates to offset costs associated with stricter capital requirements
The Commonwealth Bank to lift its home loan interest rates independently of the Reserve Bank to partially offset costs associated with stricter ...

Washington Square Signs Spielbergs, Choi Joins Variable and More
... films and an episode of the HBO comedy series "Girls," on which he plays the role of Ray Ploshansky. New York-based creative collective Variable ...

YouTube v10.32 Tweaks Some Interface Elements And Includes Hints About Virtual Reality Content And Variable ...
Forget Update Wednesday! Monday is the new big day. Not only were we treated to the latest M preview release and the official Marshmallow name, ...

"You have to ignore many variables to think women are paid less than men. California is happy to try." ...
Writes Sarah Ketterer in The Wall Street Journal in "The ‘Wage Gap’ Myth That Won’t Die" (which you can get to without subscribing if you Google ...

Expert Reveals the Latest Variable Capacity Technology
Learn about gForce Ultra CRAC equipment, the latest variable capacity technology to lower energy usage and increase reliability.

AD/CVD ruling and overseas capacity expansions are variables to the current PV boom, says EnergyTrend ...
China's National Reform and Development Commission has announced that there will be no significant reduction in feed-in tariff (FiT) rates for ...

Resources last updated: 12/23/2015 5:23:03 AM