Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx_beginners thread: Creating a simple counter. Should I use XML or Application variable or class ...?


Message #1 by "dave" <dougwood@a...> on Sun, 5 Jan 2003 05:38:18
I have a Calendar control with all daily events stored in an XML file. 
When a user clicks on a date, the daily events are displayed using a 
DataGrid/DataView.
I need to give each event a unique 'ID' number so that I can 
add/delete/update events safely.
so I have to create a 'counter' variable,so that each time a user adds an 
event to the calendar, i can just increment the varaible by 1 and assign 
the new event ID # to that event. no problem.

My question is where should I create and store this counter variable ?
In an Application variable, or maybe an XML file, or create a 
counter 'class' ?  
I guess I'm just looking for the best/correct method that expereienced 
programmers would use.

I'm so new to programming that any advice would be great !!

Thanks!
Message #2 by "Alex Smotritsky" <alex.smotritsky@v...> on Sun, 5 Jan 2003 00:35:58 -0500
I suggest not using application variables as they won't scale across
multiple servers -- session variables will scale...


-----Original Message-----
From: dave [mailto:dougwood@a...] 
Sent: Sunday, January 05, 2003 5:38 AM
To: aspx_beginners
Subject: [aspx_beginners] Creating a simple counter. Should I use XML or
Application variable or class ...?


I have a Calendar control with all daily events stored in an XML file. 
When a user clicks on a date, the daily events are displayed using a 
DataGrid/DataView.
I need to give each event a unique 'ID' number so that I can 
add/delete/update events safely.
so I have to create a 'counter' variable,so that each time a user adds
an 
event to the calendar, i can just increment the varaible by 1 and assign

the new event ID # to that event. no problem.

My question is where should I create and store this counter variable ?
In an Application variable, or maybe an XML file, or create a 
counter 'class' ?  
I guess I'm just looking for the best/correct method that expereienced 
programmers would use.

I'm so new to programming that any advice would be great !!

Thanks!

Message #3 by "dave" <dougwood@a...> on Mon, 6 Jan 2003 04:55:52
> I suggest not using application variables as they won't scale across
multiple servers -- session variables will scale...

thanks, but how would a Session variable work with something like an Event 
counter that I'm using. it doesn't really have anything to do with a 
particular Session of the user.  it's just a variable that increments each 
time any user adds a Calendar event.
Storing it in a Session variable isn't really ideal, is it ??
Message #4 by "Alex Smotritsky" <alex.smotritsky@v...> on Mon, 6 Jan 2003 00:29:46 -0500
You're exactly correct, a session variable would be useless for an
application wide thing like that. I would do it thru the db. Either
insert a record each time. Or insert a record for each time block your
interested in and then for that time block, select the record, increment
a numeric field and update. Put the sql in a stored procedure for
efficiency.


-----Original Message-----
From: dave [mailto:dougwood@a...] 
Sent: Monday, January 06, 2003 4:56 AM
To: aspx_beginners
Subject: [aspx_beginners] RE: Creating a simple counter. Should I use
XML or Application variable or class ...?


> I suggest not using application variables as they won't scale across
multiple servers -- session variables will scale...

thanks, but how would a Session variable work with something like an
Event 
counter that I'm using. it doesn't really have anything to do with a 
particular Session of the user.  it's just a variable that increments
each 
time any user adds a Calendar event.
Storing it in a Session variable isn't really ideal, is it ??

Message #5 by "Peter Lanoie" <planoie@n...> on Mon, 6 Jan 2003 09:56:34 -0500
Dave,

Seeing as the information you are inserting is living in an XML file, you
should be able to store your 'counter' value inside that.  Technically, by
having events records in there you already have the 'counter' value stored.
You could store a separate value in the XML file and deal with that or
better yet, when you save the event to the XML file, you can just look thru
the existing records for the last (highest) entry number and increment that.

A problem you might face is that you could have two processes accessing the
file at the same time, and the potential for a conflict between to records
wishing to use the same 'latest' ID.  You should probably take this into
consideration ESPECIALLY when dealing with the file system, and investigate
some kind of file locking scheme.  Like others suggested, you could do this
all in a DB and not have to deal with many of these issues.  The DB will
provide auto incrementing fields, and handle the issue of conflicting
records.

Other notes:
Application variables:  What happens when the application is restarted?
You'll have to prefill the counter to keep it up to date.
Session variables: Someone posted that the session variables will scale
across multiple servers, sure, if you are using a DB for session storage.
Otherwise, it's still living on one box, and as you pointed out, only
applicable to one user, so it is useless.
You inquired about a "counter 'class'".  Classes are essentially definitions
of 'things', not an instance of that 'thing', but even then, you would need
to store that 'thing' somewhere to be useful in your application.

My recommendation is to do this in a DB.  If people are going to be
accessing (both reading and updating) this regularly, it makes sense to use
the power of the database to do it.  I have found that XML is great for
certain applications (like transferring data between systems, or storing
more long term static data) but what you're doing warrants a database.

A total alternative suggestion to the 'counter' method is use MS GUIDs.  You
could use that to identify each unique record.  You wouldn't have to worry
about keeping track of anything yourself, just let the GUID generation deal
with that seeing as they are supposedly "globally unique to all time and
space" or something like that! ;-)  Use System.Guid.NewGuid() to create a
GUID for your record.

Good luck.
Peter

-----Original Message-----
From: dave [mailto:dougwood@a...]
Sent: Sunday, January 05, 2003 05:38
To: aspx_beginners
Subject: [aspx_beginners] Creating a simple counter. Should I use XML or
Application variable or class ...?


I have a Calendar control with all daily events stored in an XML file.
When a user clicks on a date, the daily events are displayed using a
DataGrid/DataView.
I need to give each event a unique 'ID' number so that I can
add/delete/update events safely.
so I have to create a 'counter' variable,so that each time a user adds an
event to the calendar, i can just increment the varaible by 1 and assign
the new event ID # to that event. no problem.

My question is where should I create and store this counter variable ?
In an Application variable, or maybe an XML file, or create a
counter 'class' ?
I guess I'm just looking for the best/correct method that expereienced
programmers would use.

I'm so new to programming that any advice would be great !!

Thanks!

Message #6 by "dave" <dougwood@a...> on Tue, 7 Jan 2003 01:54:30
Alex and Peter,
thanks very much for taking the time to explain. You have been VERY 
helpfull.
Think I'll use a DB !!

  Return to Index