1

I'm building a website in ASP.NET WebForm and I want to use a static type variable to hold data and use it in different classes.

To avoid the variable to reset, I declared the variable in a static type class, like this:

public static class Variavel
    {
        static int[] array = new int[30];
    }

I would like suggestions on how to use the variable in other classes particularly in classes like public partial class WebForm1 : System.Web.UI.Page without losing the data.

  • Proceed with extreme caution here. Static classes and static members are the same for all users. In most cases you do not want separate users accessing the same variables, and in those rare cases that you actually do want that, you have to write all the synchronization logic that controls when and how changes to those variables are done. – user1429080 Jan 26 '21 at 15:33

1 Answers1

0

if you want to store data in web forms, I suggest you use a session. If you use static, you will probably have big problems when your app going to be used, so use static only for global and constant information and not variable data. To use sessions in web forms you can do that like this example:

//textbox1 and textbox2 are controls of webform
Session["name"]=TextBox1.Text;
Session["email"]=TextBox2.Text;

Response.Redirect("form2.aspx");
  • this is an example of my code ```Session["array"] = new int[30]; int[] array = (int[])Session["array"]; if (button13.Checked) { cont++; array[2]++; }``` –  Jan 26 '21 at 15:43
  • In the end of your code, you need to put your new value in session like: ```Session["array"] = array```. This will put the new value of your array after your if condition in the session – Leonardo Gasparini Jan 26 '21 at 15:47
  • This [link](http://www.macoratti.net/aspn_pvw.htm) shows a full example to how you can persit data in your web forms application and handle your sessions. Maybe will help you too – Leonardo Gasparini Jan 26 '21 at 15:50
  • Does it need to be static? How do I put it as static? –  Jan 26 '21 at 16:00
  • Well, in that case i suggest to you use sessions to handle this because ```sessions``` don't reset as ```Server.Transfer```. Also, the can't be static, because seeing your code, if you use a static variable and two persons or more are using your web application in the same time, your static variable will not handle this, because they are not thread safe. [Here](https://stackoverflow.com/questions/14154892/scope-of-static-variable-in-multi-user-asp-net-web-application/14154956) is a post explaining that you need to be careful to use static variables in web apps – Leonardo Gasparini Jan 26 '21 at 16:07
  • The thing is I'm already using sessions and its not working. My website is uploaded and i am testing it and its reseting. –  Jan 26 '21 at 16:09
  • Code starts with: ```Session["array"] = new int[30]; int[] array = (int[])Session["array"]; if (button13.Checked) { cont++; array[2]++; }``` and ends with ```Session["array"] = array; Server.Transfer("WebForm2.aspx");``` –  Jan 26 '21 at 16:11
  • Ah. Your sessions is reseting because you are setting a new array every time in the beginning of your code. try to check if your session is empty before like: ```if(Session["array"] == null) {Session["array"] = new int[30] }``` – Leonardo Gasparini Jan 26 '21 at 16:14
  • Hello! Sorry to bother but the problem actually persists. But what is strange is, the website keeps the data for a certain period of time. After some time, the variable resets but its not instantly or when a new user comes.I find this very odd. –  Jan 26 '21 at 20:23
  • Should I declare the variable like ```if(Session["array"] == null) {Session["array"] = new int[30] }``` in a static method? Cause I was declaring it in the button_Click event handler. Or maybe in the Page_Load method? –  Jan 26 '21 at 20:28
  • Probably you should do that in the page_load method, because you will handle that every time that your page are showed to the user. If you put this code in a static method, you will have the same concurrency problem that I mentioned before – Leonardo Gasparini Jan 26 '21 at 23:09
  • ```protected void Page_Load(object sender, EventArgs e) { if (Session["array"] == null) { Session["array"] = new int[30]; } } ``` thats my method and it still resets after a while –  Jan 26 '21 at 23:10
  • In that case, you can put a custom config of your sessions, setting this in your web config like this ``` ``` – Leonardo Gasparini Jan 26 '21 at 23:38
  • I will try that. However, care to explain what is that? I would like to know what I'm doing other than just blindly doing it you know? This is a school project after all, the more I learn, the better. –  Jan 26 '21 at 23:42
  • Do I put that between ? –  Jan 26 '21 at 23:44
  • Sure... since that you are creating a web application, you will need to handle mutiple users using your app in the same time, besides, web applications are stateless, by default. So, to persist data exclusively for each user you need to use an different approach comparing with a desktop app or a console app. that's why you need to handle sessions or cookies in your app. – Leonardo Gasparini Jan 26 '21 at 23:47
  • And yes, you need to do that between – Leonardo Gasparini Jan 26 '21 at 23:47
  • Ok, but how does that relate to my problem and how does it apply? –  Jan 26 '21 at 23:48
  • Well to maintain a data in your web app, you need to config how long your session will be available before your app clean the sessions. changing the session state to another time will make the sessions of your app disappear only after a long time. – Leonardo Gasparini Jan 26 '21 at 23:50
  • So that time would be the time out right? –  Jan 26 '21 at 23:52
  • So what should I do if I want the data there forever? What is the config? Do I put the timeout into an absurd amount of time? –  Jan 26 '21 at 23:52
  • Forever....well, you will need a different approach, probably you will need to store that in a .txt file or a database by making a CRUD of your information. – Leonardo Gasparini Jan 26 '21 at 23:58
  • For how long can I store the information? What is the maximum? –  Jan 27 '21 at 00:01
  • Sessions are good if you want to stores information for a few minutes or hours. Besides, this information will be stored in the RAM of the machine where the app is deployed. So if the machine restarts or stop, you will lose all info stored in sessions. – Leonardo Gasparini Jan 27 '21 at 00:05
  • Machine where the app is deployed would the the host that is not my computer in this case. –  Jan 27 '21 at 00:09
  • Question is... can I set the timer to store the information for like 2 weeks? –  Jan 27 '21 at 00:09
  • Yeah. The maximum time that sessions can exist is 365 days. Convert the days that you want in minutes, which I presume is about 20160 minutes = 2 weeks. set this in the SessionState in web config and your sessions will handle that – Leonardo Gasparini Jan 27 '21 at 00:13