.NETGURU
Session state lost after accessing database
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.aspnet.caching.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

William
Hi

I have an ASP.NET application that connects to an Access database.
Everything works fine except for the Session object. Data in the session
object is lost after I've made a call to the database.

To test, I've created two test aspx pages. Test1.aspx contains two buttons.
The first button sets values in the session object and then navigates to
Test2.aspx. Test2.aspx only displays the values in the session object.
The second button creates a connection to the database, executes a SQL
statement and then does exactly the same as the first button. (Code is
copied from first button). In Test2.aspx, the values set is seen as Nothing.

For the connection to the database, I tried:
- Created a custom data class that creates the connection and provides
access to the
database. Connection is a OledbConnection connecting via OLEDB JET.
- Used the same class but changed the connection to OdbcConnection.
- Created the connection directly from the aspx page.

The SQL statement executes with no problem, but as soon as the procedure /
function has finished executing, the session data is lost. I changed the
connection to test it on a SQL Server database. Works fine. However, the
server where the application is to be deployed does not have SQL Server and
we must use an Access database. I've done something similar in old ASP
before and it worked fine.

Any help, please.

Reply to this message...
 
    
Scott Allen
Hi William:

Are you doing a Response.Redirect after setting a session variable for
the first time?

Session["SomeValue"] = 42;
Response.Redirect("test2.aspx");

The first time you write to the session for a client ASP.NET creates a
cookie to send down to the browser. On following requests the browser
sends the cookie to the server and ASP.NET can find the user's
associated session. Unfortunately, Response.Redirect ends the request
early and the session cookie is lost.

One solution is to use:

Response.Redirect("test2.aspx", false);

The second parameter tells ASP.NET not to abort the request thread
early and the session survives.

HTH,

--
Scott
http://www.OdeToCode.com

On Sat, 28 Aug 2004 18:01:03 +0200, "William" <Click here to reveal e-mail address>
wrote:

[Original message clipped]

Reply to this message...
 
    
William
Hi

Actually I've been using Server.Transfer and also tried just a normal
hyperlink. I can even have the two buttons on the same webform. One button
updates values in the session state and executes a SQL statement while the
other just displays the session values in a label control and the session
values will still be lost.

Regards

William

"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
[MSFT] (VIP)
Hi William,

How did you set the session configration in web.config? Additionlly, if we
didn't redirect to Test2.aspx, just display the sessiob variant in same
page (Test1.aspx), like:

Response.Wrtie(var);

Will this work fine?

Luke

Reply to this message...
 
    
Yan-Hong Huang[MSFT] (VIP)
Hi Viper,

I was reviewing this issue thread. Do you have any more concerns on it? If
there is any question, please feel free to post here and we will follow up.

Thanks.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
Register to Access MSDN Managed Newsgroups!
-http://support.microsoft.com/default.aspx?scid=/servicedesks/msdn/nospam.as
p&SD=msdn

This posting is provided "AS IS" with no warranties, and confers no rights.

Reply to this message...
 
    
William
Hi

Still not resolved. Web.config specifies session type as InProc.

I've also tried a single page with two buttons and a label control:
When clicking the first button, a variable is stored in the session object
and a call is made to the database (plain SELECT statement). When clicking
the second button, the variable in the session object is written to the
label control.
Currently, when clicking the second button, the variable returns Nothing. If
I comment the call to the database out and run the program again, the value
displays in the label when clicking the second button.

You can download the TestSite source code from
http://www.peopleres.co.za/temp/TestSite.zip. The zip file contains the
whole folder layout with the database and source files.

Regards

William

"Yan-Hong Huang[MSFT]" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
[MSFT] (VIP)
Hi William,

I download your code and reproduced the problem also on my server. After
some research, I found the proble m is related the location of Access
database file. You put it in the Bin folder with the .NET assembly. This
may cause the exception in oledb provider and cause ASP.NET current worker
thread exit. This cause the session variant lost. To get around the
problem, I suggest you may change the database's location, for example just
in the TestSite folder.

Luke

Reply to this message...
 
    
William
Thanks. This finally works.

"[MSFT]" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Yan-Hong Huang[MSFT] (VIP)
Hi William,

We are glad that it works now. Just one more thing, do remember not to put
this type of file in bin folder. In ASP.NET, the server will monitor bin
folder. If any of the files in this folder is changed, asp.net will think
of the application is changed and the web application will be reloaded. So
this folder only contains .dll files.

Just my 2 cents. Thanks very much.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
Register to Access MSDN Managed Newsgroups!
-http://support.microsoft.com/default.aspx?scid=/servicedesks/msdn/nospam.as
p&SD=msdn

This posting is provided "AS IS" with no warranties, and confers no rights.

Reply to this message...
 
 
System.Data.Odbc.OdbcConnection




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification