We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Application Variable

JRGirl
JRGirl asked
on
Medium Priority
349 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

Comment
Watch Question

Commented:
This is ASP.NET or Winforms?

Author

Commented:
asp.net web applications
sorry i didnt mention that
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

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

Commented:
(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

Author

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

Commented:

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.

Author

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

Commented:
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
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.