Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

session variable lost after database connection, even with response.redirect("....", false

Status
Not open for further replies.

mmy1981

Programmer
Apr 2, 2007
164
BE
Hi,
I'm having some issues holding a session variable after a response.redirect

The code below works perfectly (the Response.Redirect has the parameter false so the code or thread in default.aspx isn't stopped.
Code:
[u]default.aspx[/u]
Session("test") = "ok"
Response.Redirect("home.aspx", false)

[u]home.aspx[/u]
Label1.Text = "session var test = " & Session("test")

However, if I open a connection to a database (ex. access) and close it (I'm not doing any query action on it in my testing code, just opening and closing it) then the session variable gets lost.
Code:
Imports System.Data.OleDb

[u]default.aspx[/u]
dim objConnection As OleDbConnection = New OleDbConnection(MyConnectionString)
objConnection.Open()
objConnection.Close()
Session("test") = "ok"
Response.Redirect("home.aspx", false)

[u]home.aspx[/u]
Label1.Text = "session var test = " & Session("test")

I could work with cookies to store the info, but I can't seem to figure out why this session var isn't stored.
Even, in a different web application (created in VS2010, now using VS2012) I have almost the same code without any problems (application properties seem the same, web.config, ...).
All testing is done on my localmachine, using the webserver of VS2012
 
I see no reason why that would happen. I would recreate both pages to test again. Are there any errors when you run it, like the connection is not made?
 
Nope, I created the project again, without success.
The connection is fine, no errors, ... I stripped all the code (even re created it) to the code above.
When I comment out the objConnection.Open() and .Close() it works without a problem.
 
I was not able to reproduce your issue. I used the same except for the oledb connection,i used a sqlconnection.

I suspect you may have some setting in the web.config that may be causing it. Check sessionstate and cookiesLess = ???
 
jbenson, thx for the help.
I was sure I was able to reproduce the error in a new webapp. However, in a new project this test code seems to work. So I'll have to check the project again.
The web.config is default and does not contain anything concerning sessionsetate & cookieLess,...

I'm using cookies for the moment. I'll try to change the project/test tomorrow. Thanks anyway, at this moment I'm sure this code should work in normal circumstances
 
OK, glad it is working, i couldn't see any reason it wouldn't unless the web.config was changed. Post back if you have any issues.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top