?
Solved

Why does my session variable remain null?

Posted on 2009-04-30
11
Medium Priority
?
981 Views
Last Modified: 2012-05-06
The attached code should take the CommandArgument, assign it to a session, and redirect to the specified page.  On the specified page, a form loading in an iframe gets the session variable.  If the session variable is null, the form is programmed to display a blank page.  Right now, it's displaying a blank page, so I'm assuming that means the session variable is remaining null.

This all worked fine when I was staying in C# with my test pages.  When I converted the function SetSessionVariable to VB and plugged it into one of our actual web pages, I started having this trouble.  Below is code for an entire page so you can see where the function is and where the linkbutton is.

Let me know what else you need.

Jeremy
<%@ LANGUAGE="VBSCRIPT" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Linq" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
 
<script runat="server" language="vb">
 
Protected Sub SetSessionVariable(ByVal sender As Object, ByVal e As CommandEventArgs)
	Session("emailUser") = Server.HtmlEncode(e.CommandArgument.ToString())
	Response.Redirect("/apps/email_form/email_form.aspx")
End Sub
 
</script>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Frederick County, VA -- Information Technologies / Management Informaton Systems</title>
<link rel="shortcut icon" href="/FAVICON.ICO" type="image/x-icon" />
<link rel="stylesheet" href="/css/css-content/fredCoStyles.css" type="text/css" media="screen" />
<!--[if IE 6]><link rel="stylesheet" href="/css/css-content/fredCoStyles-IE6.css" type="text/css" media="screen" /><![endif]-->
<script type="text/javascript" src="/js/offsiteAlerter.js"></script>
<!-- #INCLUDE FILE="/includes/small_masthead_parts.asp" -->
<script type="text/javascript">
<!-- 
function calendarWindow(info)
{
	contentWindow = window.open(info, "aWindow", "location=0,width=900,height=700,scrollbars=yes,resizable=yes,screenX=400,screenY=400,top=400,left=400")
	contentWindow.focus()
}
//-->
</script>
<style>
.lwvTable td{padding-bottom:15px;}
</style>
<script type="text/javascript">
<!-- 
function menuVacanciesWindow(info)
{
	contentWindow = window.open(info, "aWindow", "location=0,width=650,height=655,scrollbars=yes,resizable=yes,screenX=400,screenY=400,top=400,left=400")
	contentWindow.focus()
}
//-->
</script>
</head>
 
<body>
<form id="itForm" runat="server">
  <div id="contentDiv">
     
    <!-- #INCLUDE FILE="/includes/small_masthead.asp" -->
    <!-- <img src="/dry_dock/images/small_masthead.jpg" border="0" /> -->
    	<!--<div id="contentPageContent">-->
	<div id="leftNavBox-contentPages">
		<!-- #INCLUDE FILE="/includes/itMenus.asp" -->
	</div>
    <img src="/dry_dock/images/seal-lines.jpg" border="0" style="position:absolute;bottom:2px;z-index:500;" />
	<div id="textHolder">
        
      <h2 class="pageHead">Information Technologies &amp;<br />
      Management Information Systems</h2>
      <p class="pageText">The Information Technologies and Management Information Systems staff of nine provides technical guidance, expertise, and support in the management of the County's computer and telecommunications technology. The Information Technologies staff also includes Geographic Information Systems (GIS) staff who support County departments and citizens in mapping, road-naming, and related activities.</p>
      <p class="pageText">The IBM iSeries provides the foundation that all County departments use to meet the needs of the day-to-day operation of the County. This support is provided by a software package from <a href="http://www.bai-map.com" target="_blank">Bright and Associates</a> in Jonesbourough, TN and by programs developed in-house.</p>
      <p class="subjectHeader">IT/MIS Contact Information</p>
      <p class="pageText">IT office telephone: (540) 665-5614<br />
      MIS office telephone: (540) 665-5620</p>
      <p class="pageText"><strong>Benny Tyson</strong>, <em>MIS Director</em><br />
      <asp:LinkButton ID="LinkButton1" runat="server" Text="Email Benny" CommandArgument="webmastr" CommandName="SetSessionVariable" OnCommand="SetSessionVariable" />
      </p>
      <p class="pageText"><strong>Marcus Lemasters</strong>, <em>IT Director</em><br />
      <a href="mailto:mlemaste@co.frederick.va.us">mlemaste@co.frederick.va.us</a></p>
      <p class="subjectHeader">Information Technologies Job Vacancies</p>
      <p class="pageText">Nothing at this time.</p>
	</div>  
  </div>
  <!-- #INCLUDE FILE="/includes/footer.asp" -->
  <!-- #INCLUDE FILE="/includes/googleAnalytics.asp" -->
</form>
</body>
</html>

Open in new window

0
Comment
Question by:mrcoulson
  • 7
  • 3
11 Comments
 

Author Comment

by:mrcoulson
ID: 24271607
I am even more sure that the variable is null because I just changed the following code on my form:

if (Session["emailUser"] != null)

to

if (Session["emailUser"] == null)

and received "Object reference not set to an instance of an object" instead of my blank page.  Clearly, it is null.  Why is it null?

Jeremy
0
 
LVL 6

Accepted Solution

by:
hehdaddy earned 1400 total points
ID: 24271932
Have you thought about making the page a traditional ASPX page? That is, an .aspx for your page and .aspx.vb for your code?
0
 

Author Comment

by:mrcoulson
ID: 24271984
That would be the best thing, but there are several hundred pages that will require such conversion.  I need to a band-aid for now.  Later in the year I plan to convert every page to C# with a codebehind.  

Jeremy
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 6

Expert Comment

by:hehdaddy
ID: 24272420
Hi there. You are using [] in your Session check. If using VB.NET you need to use ().
0
 

Author Comment

by:mrcoulson
ID: 24272525
The form checking for the variable is in C#.  Does the problem lie in the fact that the variable is set on a VB page, but read on a C# page?
0
 
LVL 6

Expert Comment

by:hehdaddy
ID: 24274988
I am not sure about that one. I will have to check on that.
0
 

Author Comment

by:mrcoulson
ID: 24294751
Just to test, I added a label on the page and commented the redirect out.  The label displays the correct text when the link is clicked; therefore the problem definitely happens in the transition between pages.  The variable ends up null after its journey to the second page.


Protected Sub SetSessionVariable(ByVal sender As Object, ByVal e As CommandEventArgs)
	Session("emailUser") = Server.HtmlEncode(e.CommandArgument.ToString())
	' Response.Redirect("/apps/email_form/email_form.aspx")
	Dim strTestString As String
	strTestString = Session("emailUser").ToString()
	lblTesting.Text = strTestString
End Sub

Open in new window

0
 

Author Comment

by:mrcoulson
ID: 24294832
I also get the same null variable if I make the first page C#.  See attached code.
<%@ Page Language="C#" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
 
<script runat="server" language="c#">
 
protected void SetSessionVariable(object sender, CommandEventArgs e)
{
	Session["emailUser"] = Server.HtmlEncode(e.CommandArgument.ToString());
    Response.Redirect("/apps/email_form/Default.aspx");
}
 
</script>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Frederick County, VA -- Information Technologies / Management Informaton Systems</title>
<link rel="shortcut icon" href="/FAVICON.ICO" type="image/x-icon" />
<link rel="stylesheet" href="/css/css-content/fredCoStyles.css" type="text/css" media="screen" />
<!--[if IE 6]><link rel="stylesheet" href="/css/css-content/fredCoStyles-IE6.css" type="text/css" media="screen" /><![endif]-->
<script type="text/javascript" src="/js/offsiteAlerter.js"></script>
<!-- #INCLUDE FILE="/includes/small_masthead_parts.asp" -->
<script type="text/javascript">
<!-- 
function calendarWindow(info)
{
	contentWindow = window.open(info, "aWindow", "location=0,width=900,height=700,scrollbars=yes,resizable=yes,screenX=400,screenY=400,top=400,left=400")
	contentWindow.focus()
}
//-->
</script>
<style>
.lwvTable td{padding-bottom:15px;}
</style>
<script type="text/javascript">
<!-- 
function menuVacanciesWindow(info)
{
	contentWindow = window.open(info, "aWindow", "location=0,width=650,height=655,scrollbars=yes,resizable=yes,screenX=400,screenY=400,top=400,left=400")
	contentWindow.focus()
}
//-->
</script>
</head>
 
<body>
<form id="itForm" runat="server">
  <div id="contentDiv">
     
    <!-- #INCLUDE FILE="/includes/small_masthead.asp" -->
    <!-- <img src="/dry_dock/images/small_masthead.jpg" border="0" /> -->
    	<!--<div id="contentPageContent">-->
	<div id="leftNavBox-contentPages">
		<!-- #INCLUDE FILE="/includes/itMenus.asp" -->
	</div>
    <img src="/dry_dock/images/seal-lines.jpg" border="0" style="position:absolute;bottom:2px;z-index:500;" />
	<div id="textHolder">
        
      <h2 class="pageHead">Information Technologies &amp;<br />
      Management Information Systems</h2>
      <p class="pageText">The Information Technologies and Management Information Systems staff of nine provides technical guidance, expertise, and support in the management of the County's computer and telecommunications technology. The Information Technologies staff also includes Geographic Information Systems (GIS) staff who support County departments and citizens in mapping, road-naming, and related activities.</p>
      <p class="pageText">The IBM iSeries provides the foundation that all County departments use to meet the needs of the day-to-day operation of the County. This support is provided by a software package from <a href="http://www.bai-map.com" target="_blank">Bright and Associates</a> in Jonesbourough, TN and by programs developed in-house.</p>
      <p class="subjectHeader">IT/MIS Contact Information</p>
      <p class="pageText">IT office telephone: (540) 665-5614<br />
      MIS office telephone: (540) 665-5620</p>
      <p class="pageText"><strong>Benny Tyson</strong>, <em>MIS Director</em><br />
      <asp:LinkButton ID="LinkButton1" runat="server" Text="Email Benny" CommandArgument="webmastr" CommandName="SetSessionVariable" OnCommand="SetSessionVariable" />
      <asp:Label ID="lblTesting" runat="server" />
      </p>
      <p class="pageText"><strong>Marcus Lemasters</strong>, <em>IT Director</em><br />
      <a href="mailto:mlemaste@co.frederick.va.us">mlemaste@co.frederick.va.us</a></p>
      <p class="subjectHeader">Information Technologies Job Vacancies</p>
      <p class="pageText">Nothing at this time.</p>
	</div>  
  </div>
  <!-- #INCLUDE FILE="/includes/footer.asp" -->
  <!-- #INCLUDE FILE="/includes/googleAnalytics.asp" -->
</form>
</body>
</html>

Open in new window

0
 

Author Comment

by:mrcoulson
ID: 24296207
A guy on www.asp.net forums says it's because they're in different web applications and "do not share the same session storage". Then he mentions postbackurl and PreviousPage.FindControl.

Any ideas on how that can help?

Jeremy
0
 
LVL 13

Assisted Solution

by:game-master
game-master earned 600 total points
ID: 24302089


good afternoon!

try to put your script code inside the <head> tag


<head>
<script runat="server" language="vb">
 
Protected Sub SetSessionVariable(ByVal sender As Object, ByVal e As CommandEventArgs)
        Session("emailUser") = Server.HtmlEncode(e.CommandArgument.ToString())
        Response.Redirect("/apps/email_form/email_form.aspx")
End Sub
 
</script>
</head>


0
 

Author Comment

by:mrcoulson
ID: 24304509
Okay, here's the story, morning glories.

I cannot use session because the email form and all the pages using it are in tons of different applications on the server.  Since each application has its own session state and cannot (easily) read the others' session states -- and because I am limited in experience, skill, and time -- I am abandoning the session idea altogether.

Instead, I'm using a query variable at the end of the URL.  It accomplishes the task almost as well as the session.  I will just have to live with having the query variable on the URL.  It's not a security concern; rather, I just was being picky about the way things look to the end user.

Nonetheless, points will be awarded here and there.

Jeremy
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month16 days, 4 hours left to enroll

850 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