billy_blanks
billy_blanks

Reputation: 105

Loosing session variable data from one page to the other in VB.NET

I am a bit new to VB.NET. I have a page that sets 2 session variables and does a redirect to second page. The second pages is at least using one of the session variables. I can tell because on the second page, if the session variable is not correct the user is redirected to an access denied page. The second page also uses the session variable in question. It will read it an fill a gridview based on the value of the variable. I set the variable like so

Session("ID") = Convert.ToInt32(a_value) 

and on the second page I retrieve the variable like this

a_page_variable = Session("ID")

What I find strange is that when I run this code in visual studio it works as expected but when I deploy and run it, I get 0 from my session variable instead of the true value of "a_value". I have tried a few things like making sure the data types match up from page to page and trying different ways to retrieve the variable such as

Session("userID")

and

CType(Session.Item("userID"), Int32)

I've also tried to see what is coming in to the second page by using Response.Write I also tried to use SQL Profiler to see what kind of call is being made to fill the gridview but I haven't had any luck. The gridview gives me an empty dataset and the Profiler does not detect a call being made from the application. I thought working with session variables was pretty straight forward but obviously, I am missing something.

Thanks for your help, Billy

Upvotes: 0

Views: 1220

Answers (2)

billy_blanks
billy_blanks

Reputation: 105

Thank you ADyson, Stephen Wrighton and everyone else who took a stab at helping me figure this out. I was able to find out what was going on by adding code that wrote to a log file on the server. Found the logging code here. I found that I never reached the code that set the session variable and that is the reason it never populated on the second page. I was trying to get the logon name for the user by using Environment.UserName which will return the user name of the person who is currently logged on to the operating system. But what I really wanted to do was get the logon name of the user that was visiting my site. For this I used User.Identity.Name. This works great when you need to know which user from an Active Directory domain is visiting your site.

Upvotes: 1

Stephen Wrighton
Stephen Wrighton

Reputation: 37830

One possibility (and the only one that could be guessed at with how little information we have) could be the response.redirect causing the application to terminate due to an exception.

When redirecting, you want to always pass a false, and then call complete request.

Response.Redirect(urlstring, False)
Response.CompleteRequest()

not following these steps can cause exceptions, which may drop session.

Additionally, resolve virtual paths, as some browsers (mobile especially) can see those redirects as new requests entirely, thus generating new session tokens.

Dim urlstring As String
urlstring = Page.ResolveUrl("~/default.aspx")

that said, there are a number of possible causes for this situation.

  1. Application Pool restarts
  2. App Domain restarted
  3. Code changing value unexpectedly
  4. AV tinkering with files
  5. deployed to web farm

With the description provided above, we just don't have enough information to really troubleshoot.

Upvotes: 2

Related Questions