Solved

Application Variable

Posted on 2004-04-12
9
320 Views
Last Modified: 2010-08-05
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

0
Comment
Question by:JRGirl
  • 5
  • 4
9 Comments
 
LVL 12

Expert Comment

by:dfiala13
ID: 10806264
This is ASP.NET or Winforms?
0
 

Author Comment

by:JRGirl
ID: 10806302
asp.net web applications
sorry i didnt mention that
0
 
LVL 12

Accepted Solution

by:
dfiala13 earned 50 total points
ID: 10806414
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
 

Author Comment

by:JRGirl
ID: 10806636
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 12

Expert Comment

by:dfiala13
ID: 10806724
(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
 

Author Comment

by:JRGirl
ID: 10806814
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
 
LVL 12

Expert Comment

by:dfiala13
ID: 10806870

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
 

Author Comment

by:JRGirl
ID: 10807019
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
 
LVL 12

Expert Comment

by:dfiala13
ID: 10807125
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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now