0

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

10
  • how is the session storage configured in your deployed web.config? What is the deployment environment like? e.g. Is there perhaps one more than IIS server (e.g. in a cluster, where each request might be redirected to a different physical server by a load balancer - I ask this because in that scenario the servers need to share session data, perhaps stored in a database)? Without config and code info it's hard to give specific advice. Descriptions of code are useful but they are quite hard things to actually fix! Commented Mar 20, 2019 at 22:12
  • I'm kinda new to .NET and I don't know how to answer the first question. How can I find out? As far as the deployment environment, there is only one IIS server Commented Mar 20, 2019 at 22:23
  • By looking in your web.config file. c-sharpcorner.com/UploadFile/484ad3/session-state-in-Asp-Net might help you understand the different session storage options and what to look for in the file. Commented Mar 20, 2019 at 22:27
  • Thank you. I'll check that out. Commented Mar 20, 2019 at 22:50
  • 1
    ok then well we can seemingly rule that out then. As the answer below notes, there are a number of other possible causes for session issues. As it seems to be an environment problem (i.e. it works in local environment but not in the server) then it's less likely that your code is at fault. Since we don't have any visibility of your environment, it's really quite hard to do anything more than make suggestions for possible areas to search in. I haven't really got anything more to add than what's already below, sorry. Commented Mar 20, 2019 at 23:20

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your input Stephen. I've tried resolving the url and adding false to the redirect but I am still having the same issues. I am setting and retrieving the session variables in the Page_Load events on both pages.
Thank you for your input Stephen. I've tried resolving the url and adding false to the redirect but I am still having the same issues. I am setting and retrieving the session variables in the Page_Load events on both pages. This is from the first page Session("ID") = Convert.ToInt32(a_value) urlString = Page.ResolveUrl("~/User.aspx") Response.Redirect(urlString, False). On the second page, I have this a_page_variable = Session("ID") SqlSource.SelectParameters("ID").DefaultValue = a_page_variable.
Stephen, I don't have any of the items you listed going on. There is one application pool, the domain has not been restarted and the application has not been deployed to a web farm. I haven't received any messages from the AV. Is there any other information that I can give that would be help out?
Really nice answer. I have a similar program, but not the same. My session values work fine at home...however when I use them on my LAN of 6 computers at the office. The first 6 users dont have any issues and the sessions work fine...however when they change to the next 6 users and they log in to their accounts..the sessions vars become null but eventually go back to normal after logging in 2-4 more times...and odd error that I can't understand...any ideas?
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.