State Management :: How To Define A Global Variable In A Page
Oct 31, 2010
I have to define a global and unchangeable variable in a web page. I used static variable but its content is the same for all users that use the page and it is bad. I dont want use Session variable.
I'm looking for a way to declare a global variable (so all pages could see) but personal (so if one user puts some value in it, it won't matter to another user or change his value).Sessions and hidden fields won't do. I've tried working with declaring keys in Web.config but unfortunely, the keys values aren't personal, and all users share the same key value.Another way I was thinking was maybe classes, but perhaps there might be an easier solution?
I need to keep a global variable throughout the whole time the user is at my site, but do I use Application or a static class? The string variable should store a region name and my site makes a few changes depending on which region that has been set.
I read that Application was mainly for classic ASP, but I also read that a static class cannot be instantiated at runtime (e.g. when the user has logged in).
So, have I got something wrong here and which solution do I use?
Not sure if this was the forum to post under but here goes. I want to make a global variable counter that I can adjust in my code in the webforms. For example, I want to use the counter like so...
[Code]....
If anyone knows how to create this dynamic global variable or if anyone can tell me if its not possible,If it is not possible,
I want client access a data center but without use database , so I want my web app can retain a global or Application session variable, that contains the data, every client can access the same data... I am try to declare in golabl, but seem it only can store String but others ..
I added the following code to global.asax to load up several session variable when a session starts. I'm assuming that when a page goes to use the variable that I should be
1) checking at the page level that the value is valid not 0 when its not expected to be 0, not a zero-length string when its expected to have a length
2) have code at the page level that sets the values if the values have not been set as when the Session timeout, ideally putting the code in a class derived from Page and then deriving all of my pages from the new class so that the code does not have to be repeated in every page
IDE: MS Visual Studio 2008 / SQL Server 2005 Express / SQL Server Management Studio Express 2005
Skills: Beginner
Well im trying to make a custom login page(im not using ASP.net Login Control), in Admin.aspx page
UserName and Pasword are entered in textboxes , then Code-Behind file checks the UserName and Pasword against DataBase(it stores the UserName and Pasword) if both are correct Session["IsAuthenticated"] = "true" and then user is redirected to AdminArea.aspx
AdminArea.aspx checks if ( Session["IsAuthenticated"].ToString() == "false" ) then redirect to Error page..
but i get NullReferenceException on AdminArea.aspx because Session["IsAuthenticated"] is null , i dont know why ?
I even tried PreviousPage.Session["IsAuthneticated"] but still got the same error..
I debugged,and found out Admin.aspx saves value in Session["IsAuthenticated"] succesfully but AdminArea.aspx looses the Session["IsAuthenticated"]..so it gets null
My Question is how to retain value in Session["IsAuthenticated"] when page is changed (I want to retrieve value in next page from Session)?
I want to show my data in next page with well formated in table.I all ready write code for session which show data in next page.but i want to show it in table format and i also want to comapre in database for a price and show too. this is my code for next page:
[Code]....
this code show me data but as i say iwant it in table format and want to comapare session varibles in database i want to show price accoding that particular product. The Pervious page code:
I have a session variable which contains a list of elements. The user defines filters, which are sent to the Database, which fills the object with the elements that match. Then, that list is shown in a table. The code goes something like this:
[Code]....
Inside that page there is a button, which sends the request given the user filters. The Server creates the object and the session variable is assigned a value.
[Code]....
It works when the user sets the filters. However, given that once done it, the session variable has a value, I don't understand why if you navigate to other pages, the table doesn't appear when you return, given that:
1. It works when you perform the query and reload the page
2. The Session variable should be set for the rest of the session
I'm working on a web page that is going to be used to enter data into a database. The page is going to be dynamic in that the user has the ability to add a row to the table with the same fields, ie lname1, fname1, mi1 then lname2, fname2, mi2
I'm wanting to use a variable to keep track of how many lines there are so that when I generate the added line I am creating a new field with the ID of lname+# . Whats the best way to do this, a global variable, session variable? As side note, first time programming in .Net or C#.
In my web application, I have to load different Master pages depending on the user log-in information. When I say different, I mean not just the colors but a whole new look[including navigation options]. So I am using 2 Master pages [one of them is set as default] and depending on the user information the other page will be used.
I got it to work by storing the user information in a Session variable and applying the master page dynamically using the Page_PreInit event. But the issue is, if I hit the back button, sometimes [yeah, only sometimes !!!] the master page is reset to the default page. Initially I thought it is a Session timeout issue. But it isn't, as some of the session variables still have data. Can anyone explain whats actually happening?
I have a web application which uses a session variable to store the logged in userid. If no user is logged in, of course this variable will be empty and the contents displayed on my website are meant for guests. If there is a user logged in, the user specific controls/access/links will then be a displayed.
I am now having issues with my hosting where on shared application pool, the worker recycle is triggered every 90 minutes, this will clear sessions causing all my users to be logged out. I opted for a dedicated application pool, which got worse because I am only allocated 50MB memory limit and if this is reached, the worker recycle is triggered and I lose my sessions again. I have tried as much as possible optimization techniques, e.g. dispose where possible, close connections, disable viewstate for static controls etc but my memory per instance keeps building up from page to page without any signs of improvement. I don't use loops nor store huge objects like bitmaps etc but my sessions are now gone even faster than 90 minutes in shared application pool before.
I have considered using SQL Session State but there isn't a simple guide on using this with MySQL. I am getting desperate and considering using a public variable, a string as a replacement to store logged in user id instead of in a session variable. I am pretty sure this will solve my issue with sessions being recycled but are there any negative consequences of doing this? One problem I can think of is if the user closes the browser, the system will never know that the user is now logged out and this public variable should be nothing. In this scenario, will the GC eventually clear this abandoned public variable.
I am building a web app that is limited to one database, therefore I cannot use the ASP.NET config tool. As a quick means of getting this app online, I tried to use a simple session variable. The login page verifies the user's credentials, and if they pass, it sets the session variable to a certain value. All other pages check in the page_load event whether or not the session variable is equal to that value. The problem is that once being logged in for a short while and sending and recieving some data, the session variable resets, and I'm returned to the login page to repeat the process. Here's the basic code from my webforms:
[code]....
Is there some setting I need to change in the web.config file to adjust the cookie timeout or is that only for the config tool authentication methods?
how to get sessions in the global.asax, but I still cant get it to work...for some reason my session throws NullExceptions..the sessions are set from another page add looks something like:
[Code]....
And once I try to get those session values I do the following in the Global.asax:
[Code]....
But as I said, for some reason I get a NullException once I try to get the values...and they do have the same session-name,so they shouldn't be null..
i dont know about Global.asax so give some information about it and also tell me about the implementation and use of session_start and session_end event. How to call Global.asax in our website.
This is quite simple. When I press btn1, it sets correctly the Session Key. And this is then the issue arrives: When I press one of these buttons, this is the sequence of what happens:
So, everytime there is a PostBack, even before the Page loads, the session starts and generates a new Id. This way, I loose everything during the PostBack. My Session State configuration:
[Code]....
This is running on VS 2010 + V3.5, on IIS5.1. The Page header configuration is default.