[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Application Variable

Posted on 2004-04-12
9
Medium Priority
?
331 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

649 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