SunQuest
           .Net Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - More.Net Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old March 5th, 2008, 03:17 PM
Budweiser Budweiser is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 40 Budweiser User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m
Reputation Power: 5
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?

Reply With Quote
  #2  
Old March 5th, 2008, 03:54 PM
Death Goddess's Avatar
Death Goddess Death Goddess is offline
Crushing a million faces
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2007
Posts: 282 Death Goddess User rank is Second Lieutenant (5000 - 10000 Reputation Level)Death Goddess User rank is Second Lieutenant (5000 - 10000 Reputation Level)Death Goddess User rank is Second Lieutenant (5000 - 10000 Reputation Level)Death Goddess User rank is Second Lieutenant (5000 - 10000 Reputation Level)Death Goddess User rank is Second Lieutenant (5000 - 10000 Reputation Level)Death Goddess User rank is Second Lieutenant (5000 - 10000 Reputation Level)Death Goddess User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 5 Days 11 h 25 m 14 sec
Reputation Power: 88
Quote:
Originally Posted by Budweiser
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).

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.

Reply With Quote
  #3  
Old March 6th, 2008, 02:58 PM
Budweiser Budweiser is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 40 Budweiser User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m
Reputation Power: 5
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.

Reply With Quote
  #4  
Old May 4th, 2008, 07:09 PM
tymberwyld tymberwyld is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 1 tymberwyld User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 m 12 sec
Reputation Power: 0
Post

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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - More.Net Development > Separate set of session variables for different page instances


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway