Solved

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

Posted on 2004-07-31
14
819 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

830 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