?
Solved

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

Posted on 2004-07-31
14
Medium Priority
?
826 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

800 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