?
Solved

Application Variable

Posted on 2004-04-12
9
Medium Priority
?
330 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
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.

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

762 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