Solved

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

Posted on 2004-07-31
14
814 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
Comment Utility
Should be Session("MyVar")

" double quotes not single '

Regards,

Aeros
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
Comment Utility
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
Comment Utility
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
 
LVL 1

Author Comment

by:roknjohn
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 9

Expert Comment

by:crescendo
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
You are correct, and I thank you for your contribution.

I have placed points for you in another thread.

Thank you.
0

Featured Post

Highfive Gives IT Their Time Back

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!

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
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…

771 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

11 Experts available now in Live!

Get 1:1 Help Now