• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 985
  • Last Modified:

Why does my session variable remain null?

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
mrcoulson
Asked:
mrcoulson
  • 7
  • 3
2 Solutions
 
mrcoulsonAuthor Commented:
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
 
hehdaddyCommented:
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
 
mrcoulsonAuthor Commented:
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
hehdaddyCommented:
Hi there. You are using [] in your Session check. If using VB.NET you need to use ().
0
 
mrcoulsonAuthor Commented:
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
 
hehdaddyCommented:
I am not sure about that one. I will have to check on that.
0
 
mrcoulsonAuthor Commented:
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
 
mrcoulsonAuthor Commented:
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
 
mrcoulsonAuthor Commented:
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
 
game-masterCommented:


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
 
mrcoulsonAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 7
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now