Solved

Session is nothing after page load

Posted on 2004-08-19
30
800 Views
Last Modified: 2008-02-07
I'm using the State Server in my web.config file. and have tried using the SQLServer also.

<sessionState mode="StateServer" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;user id=myuser;password=mypassword" cookieless="false" timeout="10"/>


I notice that when i use the SQLServer my sessions are stored in the database ASPStateTempSessions.

Now my problem is when i assign a session a value it's stored and i can write it to the page, but when i do another page load the session is lost. i see on a trace that it's being written to the database and that i think my problem lies somewhere where with each page load the sessionid changes, i'm not entirely sure why i can't read the session after i load the page or move to another. i'm using the microsoft example and paste that code below.

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="webbdata.WebForm1" enableViewState="True" EnableSessionState="True"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
      <HEAD>
            <title>WebForm1</title>
            <meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR">
            <meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
            <meta content="JavaScript" name="vs_defaultClientScript">
            <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
      </HEAD>
      <body MS_POSITIONING="GridLayout">
            <SCRIPT runat="server">
  Sub Session_Add(sender As Object, e As EventArgs)
     Session("MySession") = text1.Value
     span1.InnerHtml = "Session data updated! <P> Your session contains: <font color=red>" + Session("MySession").ToString() + "</font>"
  End Sub

  Sub CheckSession(sender As Object, e As EventArgs)
  response.write(session("MySession"))
    If Session("MySession") is nothing Then
      span1.InnerHtml = "NOTHING, SESSION DATA LOST!"
    Else
      span1.InnerHtml = "Your session contains: <font color=red>" + Session("MySession").ToString() + "</font>"
    End If
  End Sub
            </SCRIPT>
            <form id="Form1" runat="server">
                  <input id="text1" type="text" name="text1" runat="server"> <input id="Submit1" type="submit" value="Add to Session State" name="Submit1" runat="server" OnServerClick="Session_Add">
                  <input id="Submit2" type="submit" value="View Session State" name="Submit2" runat="server" OnServerClick="CheckSession">
            </form>
            <hr SIZE="1">
            <font size="6"><span id="span1" runat="server"></font></SPAN>
      </body>
</HTML>
0
Comment
Question by:simmonsusa
  • 16
  • 12
  • +1
30 Comments
 
LVL 18

Expert Comment

by:DotNetLover_Baan
Comment Utility
Whats in your code behind ?   Do you have one ?
-Baan
0
 

Author Comment

by:simmonsusa
Comment Utility
No there is no code behind, I'm using .net framwork 1.1 on a windows 2003 server machine and visual stuidio 2002
0
 
LVL 21

Expert Comment

by:tovvenki
Comment Utility
Hi,
I think that when you say mode="StateServer" then you need to start the aspstate service in the PC. are you sure that this service is running in you PC. if not then go to command prompt and type
net start aspstate

Regards,
venki
0
 

Author Comment

by:simmonsusa
Comment Utility
yes this service is started, as mentioned also, the SQLServer session is being saved into the database but when the page reloads for some reason the session doesn't live.
0
 
LVL 8

Expert Comment

by:daffodils
Comment Utility
Here is your problem..

stateConnectionString="tcpip=127.0.0.1:42424"
sqlConnectionString="data source=127.0.0.1

<sessionState mode="StateServer" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;user id=myuser;password=mypassword" cookieless="false" timeout="10"/>

These two fields need your server IP address and not the default address 127.0.0.1 !

Also you can use either State Server OR SQL Server for storing Session.. not both at the same time..

Store Session in either State Server as follows (use server IP and the default port 42424)
<sessionState mode="StateServer" stateConnectionString="tcpip=<server IP>:42424" cookieless="false" timeout="10"/>

Or store Session in either SQL Server as
<sessionState mode="StateServer" sqlConnectionString="data source=<server IP>;user id=myuser;password=mypassword" cookieless="false" timeout="10"/>
0
 

Author Comment

by:simmonsusa
Comment Utility
The mode should be either SQLServer or StateServer, I'm having problems with both. the ip address should be correct with the 127 cause it all resides on the same machine, but just to test it i did go ahead and put the machine ip address in there instead but still have the same problem.

Please help.
0
 

Author Comment

by:simmonsusa
Comment Utility
Just to clarify when i click the Add session button i can read the session, but when i click the View session button it is lost.
0
 
LVL 8

Expert Comment

by:daffodils
Comment Utility
Lets start with SQL Server first..

Run InstallSqlState.sql (installed by default in <Drive>:\systemroot\Microsoft.NET\Framework\version) on the computer running SQL Server that will store the session state. This creates a database called "ASPState" with new stored procedures and ASPStateTempApplications and ASPStateTempSessions tables in the TempDB database.

In the application's Web.config file, set mode=SQLServer and set the sqlConnectionString attribute.

For example --> If they are all on the same machine.. and you are using windows authentication, set connection string as :
sqlConnectionString="data source=localhost;Integrated Security=SSPI;Initial Catalog=northwind"

The sqlConnectionString value shoudl be similar to the connection string used when actually connecting to the server.
So first test if you can connect to the SQL server from code with the connection string and then place it in the Web.config.
0
 

Author Comment

by:simmonsusa
Comment Utility
Thanks mate, but i have done that, and in my original question explained that looking in at the ASPState database and using profiler can see that my session has been saved, my problem is after i reload the page or using my example click the view session button, that it has been lost. Everytime i create a session it is being stored in SQL server, but why does it get lost when i try to access the session again.
0
 
LVL 8

Expert Comment

by:daffodils
Comment Utility
I just tested your code as with the State Service running and the connection string as
<sessionState
            mode="StateServer"
            stateConnectionString="tcpip=127.0.0.1:42424"
            sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes"
            cookieless="false"
            timeout="20"
/>

And it works !
0
 
LVL 8

Expert Comment

by:daffodils
Comment Utility
Even when I Click "View Session State".. I can see the data I added !
0
 
LVL 8

Expert Comment

by:daffodils
Comment Utility
what is the Value for "Log on as" column for your ASP.NET State Service.. in the Services console?
0
 

Author Comment

by:simmonsusa
Comment Utility
Yes it should work, are you using the same software .net ver 1.1 with IIS 6, honestly after reading microsoft it shouldn't be difficult, and what i have done should work, but i dont' understand why it doesn't, that's why i'm here.
0
 

Author Comment

by:simmonsusa
Comment Utility
NT AUTHORITY\NetworkService
0
 
LVL 8

Expert Comment

by:daffodils
Comment Utility
Ahh.. I am using .NET ver 1.1 , but with IIS 5.0 with Win2K Advanced Server ..
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 8

Expert Comment

by:daffodils
Comment Utility
Hey, does it work for you "InProc" ?
The reason am asking this is.. maybe machine.config doesn't have SessionStateModule
Sounds really unlikely.. but just in case.
0
 

Author Comment

by:simmonsusa
Comment Utility
No it doesn't, well it does if i turn cookieless to true, which i had been doing but crystal has a bug when using the active X print on a form using cookieless to true, so i need to be able to get this to work. where do i find the machin.config file?
0
 
LVL 8

Expert Comment

by:daffodils
Comment Utility
C:\winnt\microsoft.net/framework/v1.1.4322/CONFIG
0
 
LVL 8

Expert Comment

by:daffodils
Comment Utility
Check for these sections...

<section name="sessionState" type="System.Web.SessionState.SessionStateSectionHandler, System.Web, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" allowDefinition="MachineToApplication" />

<pages buffer="true" enableSessionState="true" enableViewState="true"
            enableViewStateMac="true" autoEventWireup="true" validateRequest="true" />

<sessionState
            mode="InProc"
            stateConnectionString="tcpip=127.0.0.1:42424"
            stateNetworkTimeout="10"
            sqlConnectionString="data source=127.0.0.1;Integrated Security=SSPI"
            cookieless="false"
            timeout="20"
        />

<httpModules>
            <add name="Session" type="System.Web.SessionState.SessionStateModule" />

Although, it should have given an error if Session State was not enabled !
This is really wierd.. what's so different with IIS6.0 and Win2003 !!
0
 

Author Comment

by:simmonsusa
Comment Utility
thanks, seems to look ok to me.
0
 
LVL 8

Expert Comment

by:daffodils
Comment Utility
sorry.. I am out of ideas !
It should at least work "InProc" with EnableSessionState=true in the @Page directive !
0
 

Author Comment

by:simmonsusa
Comment Utility
ok, well now i'm really stumped. i thought you know, it can't be the server because the server is storing the session and i can see that using the profiler, i thought i'd go to another machine and try it, and well there you have it, it's my browser. but i've enabled everything so for some reason on my browser it won't accept the cookie.
0
 
LVL 8

Expert Comment

by:daffodils
Comment Utility
Oh.. so JavaScript is not enabled on your browser.. hmm, I think it is some security setting that turns it back on.
let me see..
0
 
LVL 8

Expert Comment

by:daffodils
Comment Utility
Try this.. IE 6.0
Tools --> Internet Options  --> Security Tab --> Custom Level (Button)  --> Scroll to SCRIPTING section  --> Use Radio button select of Enable or Disable to turn Java Script support on or off
0
 

Author Comment

by:simmonsusa
Comment Utility
I do have the active scription enabled but i still can't see it, i've tried on other win200 machines and xp machines and am having the same problem, the problem does not occur on NT machines for some reason, have you any ideas?
0
 
LVL 8

Expert Comment

by:daffodils
Comment Utility
It is a classic Browser problem.. your security settings are giving you a problem.

what do you mean by this.. if you can't see it, how do you know it is enabled.
>>I do have the active scription enabled but i still can't see it,

Lets just set the security setting lower.. that should automatically set the Javascript on...
Tools --> Internet Options  --> Security Tab --> Custom Level (Button)  --> Reset Custom settings (drop down)  --> Set to Medium or Medium-Low or Low
0
 
LVL 8

Expert Comment

by:daffodils
Comment Utility
One more thing to try.. off the top of my head.. do you have Virus Scan enabled? Try disabling that.

Here's an excerpt from an MSDN article

==When using the in-process session-state mode, session-state data is lost if aspnet_wp.exe or the application domain restarts. These restarts commonly occur in the following circumstances:
- Setting an attribute in the <processModel> element of the application's Web.config file that causes a new process to start when a condition is met, such as memoryLimit.
- The Global.asax or Web.config file is modified.
- Changes to the \Bin directory of the Web application.
- Antivirus software scans and modifies the Global.asax file, the Web.config file, or a file in the \Bin directory of the Web application.    

==If you enable Web garden mode in the <processModel> element of the application's Web.config file, do not use in-process session-state mode. Otherwise, random data loss can occur.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconsessionstate.asp
0
 
LVL 8

Accepted Solution

by:
daffodils earned 500 total points
Comment Utility
Look at these MSDN articles.. to set the security settings

How to Manage Cookies in Internet Explorer 6
http://support.microsoft.com/default.aspx?scid=kb;EN-US;283185

PRB: Session Data Is Lost When You Use ASP.NET InProc Session State Mode
http://support.microsoft.com/default.aspx?scid=kb;en-us;324772

0
 

Author Comment

by:simmonsusa
Comment Utility
I appreciate you pointing me into the right direction the answer in my situation is

http://support.microsoft.com/default.aspx?scid=kb;en-us;316112&Product=ie600

PRB: Session Variables Do Not Persist Between Requests After You Install Internet Explorer Security Patch MS01-055

My sql server is name SQL_SERVER which also hosts the intranet. thanks for all your help, if you hadn't given me that last answer i wouldn't have found this so i'll give you the points. thanks again.
0
 
LVL 8

Expert Comment

by:daffodils
Comment Utility
Oh Great ! Sure.. Thanks, am glad I could help :).
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

16 Experts available now in Live!

Get 1:1 Help Now