In my pages, the first thing I do when a page is being displayed is query a table called user preferences. I'm doing this for every page and I was wondering what are the options available to avoid repeating this query as the user jumps from page to page on my application.
Thanks.
You should either store the user preference in a Cookie or Session.
Some good reads for you:
http://wiki.asp.net/page.aspx/57/session/
and
http://msdn.microsoft.com/en-us/library/aa289495(v=vs.71).aspx
If both cases, check if the value is not null. If it is not null, use the Cookie or Session value. If it is null, populate your Session / Cookie value with the result of your database call.
This way you will only make this DB call once.
Related
I have a website with a number of aspx pages and one of them requires generating unique session id when an user comes in. This session id should be in time structured string as yyyymmddhhmmssms (year,month,day,hour,minute,second,milisecond). So when an user accesses to this particular aspx page, the user gets an unique session id. Then when finish button is clicked the session id is fired and will never be reused.
This might be possible, but first things first: A timestamp isn't unique, and you don't ever want sessionIDs to be guessable. That's why default SessionIDs are 120-bit numbers that are generated by a cryptographic random number generator (details here). Anything deterministic like a timestamp should be avoided unless you and your users are ok with the contents of a session being available to the entire world.
More about session state security is here.
Anyway, I'm not aware of a way to swap out the session ID for a single page. Your browser associates a session cookie with a host, not an individual page, so it'll send your session cookie to every page on your site. If you're not using sessions anywhere else on your site then you could get away with the following:
Disable session state for every page except the one in question by using the Page directive's EnableSessionState attribute. When the user clicks the finish button, call Session.Abandon in the handler.
Use your own session ID generator by implementing and registering your own ISessionIDManager implementation. But please, please don't use a timestamp for your IDs.
In my asp.net MVC application, I want to add a variable (a flag for the kind of system a user has) that can be accessed whenever a page loads or the user performs certain actions. I have decided to add a session variable for this (does this seem reasonable?), and I just need to grab the flag from a table in the database. My plan was to set the variable on Session_Start, but this doesn't appear to be the right way to do it as I need to query the database and I'm not sure if I should from the Global.asax. Where should I be populating this variable? Or is there a better way to do it?
Thanks in advance!
Session variable is a reasonable choice and session_start can be a place to get the value. However, if the value is user specific then in such case, you need user identity. Authentication will establish user's identity and not session start (both are independent in ASP.NET) - so instead of session_start, better bet would be Application_AcquireRequestState where you should check if user is authenticated or not and if yes, then check if your session variable has been set or not. If not set then you can get the value from database.
A slight variation would be on-demand loading i.e. create a wrapper method to get the flag value. Wrapper method will check if value has already been retrieved or not - if not then it will fetch it and cache the value in suitable store (e.g. session state).
Suppose if there are multiple sessions on single page and out of them I want to clear values of only one session then how can I achieve this. Will Session.clear() clear all sessions' value or it will create ambiguity because of multiple session?
You have only one session for one user at a time. If you clear the session, you only clear the session for the current user visiting the page, not for everybody.
Session.Clear(); // Remove all keys and values from the session state collection of current user
Session.Remove("SessionName"); //Will remove particular session variable of current user
Essentially, this is what I have:
I have a view form for an object in MVC. On this form, I have a hyperlink. Every time that the user clicks the hyperlink, I would like an integer value inside of the object to increase by one, in addition to redirecting the user to a different page.
Is there a way to do this and keep track of how many times the link is clicked?
Thanks for any help that you can offer!
Yeah. Just have the link point to an MVC action that increments the value you want to increment before redirecting them to the action that they should actually go to.
Without more information, that's about as detailed as I can get.
The options are many. If it is one user then you could track this in any of the following: session, cookie, querystring, database or even LocalStorage.
If "the user" is just any user then cookies and LocalStorage are ruled out.
I'd imagine session is the easiest, assuming scale is not a major concern.
You have multiple options for this.
-> Save it in database
-> Session
-> increment value in the global variable
I have a web application which has a Sql Server database on the backend. As the site will be rolled out to different clients (or different virtual directories in IIS), each client derivative of the site will use the same backend.
There is an admin page where text on the site can be changed (enter text in a listview, and choose the page to select where that text will show up, and also you can see company-specific details in the other listviews. As this is a shared database, that means a client can see each other's data.
What I am trying to do is store the accountId (a guid returned from the database from login_authenticate), and stick this into session. I then retrieve this on the admin page, and I want to use this value (But it's always 0000-0000 etc), to limit the records returned in the listview.
Is there an example of this? Also, how can I set the default value (this is in the where clause of SqlDataSource), to programatically what the account id is (so I can give me all records = what the current accountid is, or perhaps, what the login is - this is stored in the account table).
Thanks
This is what I tried.
What I am confused about, though, is whether the where clause, when using a session object, is getting an object that I have written the code to retrieve from the session, or an object I have only added but not retrieved. I get the accountID when logging in (verified via stepping in - obviously - or the login will fail).
I will try again with storing the object in session # the login page when I have just retrieved the accountid variable, and then retrieve it on another page.
For some reason I keep getting 0s so I will look at this in my application.
It sounds like your method should be working. I would follow a debugging process:
Check that you are getting the accountID value from the database. Print it on screen immediately after retrieving the value for the first time.
If this is working, store the value in the Session and immediately retrieve it, and check that you are getting the value back.
Create 2 test pages, one where you set the Session variable and another where you retrieve it.
I know this seems really basic, but the failure is being introduced somewhere in the above 3 places. If you can find which step fails, you will be able to fix it.