Application Variable

pleaseeeeee i need to know how can I create an application variable that is static among all users using the system..
all my tries werent succesful
thanx
JRGirl

JRGirlAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
dfiala13Connect With a Mentor Commented:
Very simple:

To set...

Application["MyVar"] = 3; //or whatever value you want, these will hold any object

To retrive...

int i = (int)Application["MyVar"] ; //just need to cast it on the way back.

see this for more info
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebhttpapplicationstateclasstopic.asp

Just be careful, using globals can be lead to surprising results if everyone is updating them frequently.  Good idea to restirct use of them to fairly static data.
0
 
dfiala13Commented:
This is ASP.NET or Winforms?
0
 
JRGirlAuthor Commented:
asp.net web applications
sorry i didnt mention that
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
JRGirlAuthor Commented:
Hi
Thanx a lot just a question
in c sharp is it like a standard to use this syntax
(int)Application...
or
(string)Application
to formate the retrieved application variable??
and please? like can u guide me to a way I can work smoothly with asp.net and c#... ok well I see the msdn library there but i dont know how to use it right away.. thats my problem i guess i just dont know from where i can find answers and also where should i look first when i find a problem...
i just go to google and search :( and i dont have much time left i have to submit my work
anyway
thanx
JRGirl
0
 
dfiala13Commented:
(int)Application...
or
(string)Application

are just examples of casts.  You need to cast an object to its proper type in C# to use it.  If you stored an int, use (int), if you stored a string, use (string), etc.

>>i just go to google and search :( and i dont have much time left i have to submit my work
This isn't for homework is it?

My links page has a number of good starting points...

http://www.vpsw.com/links.aspx
0
 
JRGirlAuthor Commented:
loool its not a homework... its a Senior Project... and am developing this for a company... but its not wrong to ask questions... hmm right? I just asked u how to do the app vars and am gonna takecare of my self after :)
well not yet,
I still need to declair an application variable before intializing it
is it possible
thanx
0
 
dfiala13Commented:

This is all you need to do...

Application["MyVar"] = 3;

It creates an application var named "MyVar" if one doesn't exist or updated "MyVar" if it does.
0
 
JRGirlAuthor Commented:
ok here is the problem am facing in detials...
I have for example table with two attribles ID and Flag
ID takes the value of a sequence from my oracle database called sq1 <<like autonumbers in access>>
I need to retrieve the Id of the current record I inserted so I do this

1- insert into Images Values(sq1.nextval,'X')            
where sq1.nextval is a sequence am using in oracle as the auto Id for the record and X is the flag
then
2- select * from images where flag='X'
to obtain the record that have the falg as X by this am getting the sq1.nextval that was just generated
3- update images set flag='O' where flag='X'
then I sent the flag to O so this means that this record is ok and I know the value of the ID

which works just fine!!! BUT if two users came at the same time and wanted to insert this product
I can run into the possiblity that there are two records set to flag X which means the ID am retrieving is wrong! :S
so I thought of the way to solve it by setting a global variable>>application varaible called Lock takes 0 or 1

int i = (int)Application["Lock"];
if (i==1)
while(i==1){}
else{
Applicataion["Lock"]=1;
1...
2...
3...
Applicataion["Lock"]=0;
}

so if user A came and tried to execute the 3 lines first we check if another user lets say User B is executing right now those 3 lines by the if (i==1) condition
if so then loooool we let User A enter in the loop while(i==1){}  until he get dizzy and User B is done with those 3 statements...
and when user B exectute the last line Applicataion["Lock"]=0; then User A can get his turn


now I did that but because I didnt declair or gave any value for Application["Lock"] before I checked its value
this exception was thrown to me
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
u see what am saying now???

long question sorry but I think its fun too

JRGirl
0
 
dfiala13Commented:
As I mentioned in my original post, you don't want to use application variables in this way.  You'd be better off using session variables.  They work the same way as application variables but are only global to the specific user on the session.

Your concurrency issues with the Sequece id should be handled in Oracle.  ie you should never get the same ID from the database.

As to locking a section of code, there are better ways to do it.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfLockStatement.asp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.