Solved

Session is nothing after page load

Posted on 2004-08-19
30
802 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
ID: 11847338
Whats in your code behind ?   Do you have one ?
-Baan
0
 

Author Comment

by:simmonsusa
ID: 11847462
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
ID: 11849321
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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

Author Comment

by:simmonsusa
ID: 11852065
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
ID: 11852222
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
ID: 11852315
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
ID: 11852338
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
ID: 11852398
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
ID: 11852459
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
ID: 11852461
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
ID: 11852478
Even when I Click "View Session State".. I can see the data I added !
0
 
LVL 8

Expert Comment

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

Author Comment

by:simmonsusa
ID: 11852525
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
ID: 11852541
NT AUTHORITY\NetworkService
0
 
LVL 8

Expert Comment

by:daffodils
ID: 11852587
Ahh.. I am using .NET ver 1.1 , but with IIS 5.0 with Win2K Advanced Server ..
0
 
LVL 8

Expert Comment

by:daffodils
ID: 11852628
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
ID: 11852676
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
ID: 11852744
C:\winnt\microsoft.net/framework/v1.1.4322/CONFIG
0
 
LVL 8

Expert Comment

by:daffodils
ID: 11852805
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
ID: 11852813
thanks, seems to look ok to me.
0
 
LVL 8

Expert Comment

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

Author Comment

by:simmonsusa
ID: 11852901
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
ID: 11853018
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
ID: 11853033
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
ID: 11853324
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
ID: 11853649
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
ID: 11853770
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
ID: 11853906
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
ID: 11854324
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
ID: 11854464
Oh Great ! Sure.. Thanks, am glad I could help :).
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

803 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