Link to home
Start Free TrialLog in
Avatar of roknjohn
roknjohn

asked on

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

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  

Avatar of AerosSaga
AerosSaga

Should be Session("MyVar")

" double quotes not single '

Regards,

Aeros
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;
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#.
Avatar of roknjohn

ASKER

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!  
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.
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?


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.
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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".
You are correct, and I thank you for your contribution.

I have placed points for you in another thread.

Thank you.