|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
Separate set of session variables for different page instances
Hi,
I am developing a .Net tool in which I need to store a datatable in a session variable. There are only a handful of users of the system at one time, so there's not likely to be any resource problems. However, one user may wish to view the same page in two different browser windows at the same time, to make changes to 2 different database records. There will be a clash here because both pages will be trying to use the same session variables. I came up with the idea of prefixing the session variable names with a unique id stored in a hidden field, based on the time. So when not postback, I set the hidden field to a value like 201427 (when the time is 20:14:27). I then realised that it falls apart in page.init as the viewstate is not available. How can I either a) get a unique id for each page or b) separate the session variables in some way between multiple instances of the same page? |
|
#2
|
||||
|
||||
|
Quote:
That's a really bad, really hackish way to do things, and I guarantee the next developer who maintains your application is going to very upset seeing code like that. It sounds like the problem stems from storing the datatable in the session, which is normally an indication of bad application design. I usually find it easiest to help people when I know what they are trying to do. So, what exactly are you trying to do that requires keeping a datatable in the session (as opposed to re-retrieving data from the database)?
__________________
Baby soft, because its made from real babies. |
|
#3
|
|||
|
|||
|
The system I am building has a list of hotels. Each hotel has a list of date ranges, with different prices. This list of date ranges is dynamic - there could be zero, there could be 100.
When the user is editing a hotel, they can edit its name, address, etc. and they can add/edit/delete these date ranges (and their prices, etc.), but I don't want to commit anything to the database until the user has clicked 'Save' for the hotel. That way, if the user abandons their changes to the hotel, the datatable is simply lost. And if they click save, the datatable is used to update the real database. So I've opted to keep a separate datatable with the date ranges, which is passed to each postback by storing it in a session variable. The problem with this is that if the user has more than one window open, the datatable from one window's session is of course available to the other windows too. Not sure how else to do this really. |
|
#4
|
|||
|
|||
|
Well, all you should need to do is store your DataTable variables in the Session state using the Database Key:
string sessionKey = string.Format("HotelData:{0}", dbID); DataTable dtData = (Session[sessionKey] as DataTable); if (dtData == null) { // Load data from Database dtData = DAL.LoadData(); // And set Session state variable Session[sessionKey] = dtData; } // Bind data This way, if your User goes away from the Page (without saving), but opens it later, the data would still be there for that key (which could be bad). I recommed clearing those Session variables once the User has saved the data, or if they navigate away from that page. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > .Net Development > Separate set of session variables for different page instances |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|