Solved

Session variables not persisting (IIS 5.0 & ASP.NET)

Posted on 2004-07-31
14
817 Views
Last Modified: 2011-04-14
On a Win 2KPro machine w/ IIS 5.0, I am building a test ASP.NET application with Delphi 8.  It appears that my session variables are not persisting between page requests.  However, the Session.SessionID remains the SAME.  In other words, each time the page is loaded, the SessionID is the same, but the variables are gone.

My Delphi 8 example (below) sets a session variable "MyVar" which is subsequently lost when the page is reloaded.  I originally thought my problem was in the code, but I suspect it lies in my IIS & ASP.NET configuration.

Could any post a link to a SIMPLE sample project that uses asp.net & session variables that I can load onto my IIS?  This way I can be sure it is not Delphi related.  Thanks.


Here what I've done, so far:
I set my IE browser to 'Accept All Cookies'. (It was set to 'Low').
I created a new ASP.NET application, using all default names. (WebApplication1,etc.)
I added 3 labels and 2 buttons to WebForm1.
Here is the code that I added:

procedure TWebForm1.Page_Load(sender: System.Object; e: System.EventArgs);
begin
  Label1.Text := 'Session Id = '+Session.SessionID;
  if Session['MyVar']<>nil then
    Label2.Text := 'MyVar = '+Session['MyVar'].ToString
  else
    Label2.Text := 'MyVar is undefined.';
end;

procedure TWebForm1.Button1_Click(sender: System.Object; e: System.EventArgs);
begin
  Session['MyVar'] := 'George Bush';
  Label3.Text := 'MyVar set to '+ Session['MyVar'].ToString;
end;

procedure TWebForm1.Button2_Click(sender: System.Object; e: System.EventArgs);
begin
  Session['MyVar'] := 'John Kerry';
  Label3.Text := 'MyVar set to '+ Session['MyVar'].ToString;
end;

--------------------
Here is the output:

First Display:
   Session Id = bjhiwo2fgvpsgcrxxsxqk245
   MyVar is undefined.
   Label  

After clicking Button1:
  Session Id = bjhiwo2fgvpsgcrxxsxqk245
  MyVar is undefined.
  MyVar set to George Bush  

After clicking Button2:
  Session Id = bjhiwo2fgvpsgcrxxsxqk245
  MyVar is undefined.
  MyVar set to John Kerry  

0
Comment
Question by:roknjohn
14 Comments
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11685552
Should be Session("MyVar")

" double quotes not single '

Regards,

Aeros
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 11685563
Hi there... need to change the page_load  to  page_PreRender..

procedure TWebForm1.Page_PreRender(sender: System.Object; e: System.EventArgs);
begin
  Label1.Text := 'Session Id = '+Session.SessionID;
  if Session['MyVar']<>nil then
    Label2.Text := 'MyVar = '+Session['MyVar'].ToString
  else
    Label2.Text := 'MyVar is undefined.';
end;
0
 
LVL 9

Expert Comment

by:crescendo
ID: 11685643
I just tried the following code and it worked fine. Create a new blank page, add a button (Button1), a textbox (Textbox1) and a label (Label1). Then add the following code, build it and run:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Label1.Text = "Session variable was: "
        If Session("myVar") Is Nothing Then
            Label1.Text += "Undefined"
        Else
            Label1.Text += Session("myVar")
        End If
        Session("myVar") = TextBox1.Text
        Label1.Text += "<br>Session variable now set to: " + TextBox1.Text
    End Sub

Sorry it's VB.Net rather than C#.
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 1

Author Comment

by:roknjohn
ID: 11685707
Thanks for the response guys!!

Problem solved.

I re-installed IIS (after removing it) then ran the aspnet_regiis.exe -i utility.  All works well now.  

Two days shot to heck!  
0
 
LVL 9

Expert Comment

by:crescendo
ID: 11900995
Why the two-way split, not three-way? My answer seems just as valid, as he asked for sample code to prove a point, and that's what I gave.
0
 
LVL 9

Expert Comment

by:crescendo
ID: 11929817
Hi modulo

OK, but what's the point of saying, "Any objections should be posted here in the next 4 days" if those objections are going to be ignored?


0
 
LVL 1

Author Comment

by:roknjohn
ID: 11930453
none of the responses were helpful or solved the problem.  

I had to reinstall IIS to get it to work.


aerossaga resonse was completely irrelevent since I code it DELPHI, which requires single quotes.

dotnetlover was wrong because the code works fine in the page-load event (after reinstall)

cresendo posted the only correct resonse, in essence saying, that there was nothing wrong with my code.
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 11931282
Thanks for your response roknjohn.

I've PAQ'd the question and refunded your 500 points as you did solve the problem yourself.

modulo
CS Moderator
0
 
LVL 9

Expert Comment

by:crescendo
ID: 11933878
roknjohn:

Your question was, "Could any post a link to a SIMPLE sample project that uses asp.net & session variables that I can load onto my IIS?  This way I can be sure it is not Delphi related."

I did that, and it enabled you to rule out Delphi, just as requested. You didn't ask us to solve the problem as such, so it seems a bit unfair to say that "none of the responses were helpful or solved the problem".
0
 
LVL 1

Author Comment

by:roknjohn
ID: 11936023
You are correct, and I thank you for your contribution.

I have placed points for you in another thread.

Thank you.
0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

832 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