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

Session Variable question for C#


I am trying to use Session variables in an ASPX application.  For some reason the code below doesn't seem to be assigning the Session variable as it is null when I try to access it on the other page.  I have tried various code changes but nothing seems to work.  The 'thisAssID' variable is not null and is used later on the page.  So, I don't understand why the Session variable is not getting assigned properly.

Many thanks for any help!  I am not that familiar with C#.

public void Page_Load(object sender, EventArgs e)
			  if (((Request.QueryString["ID"]) != "")) {
  thisAssID = int.Parse(Request.QueryString["ID"]);
  Session["TestID"] = thisAssID;

Open in new window

2 Solutions
are cookies enabled in your web browser?
by default asp.net creates a cookie on the browser of client computer to keep track of the session.
Pratima PharandeCommented:
are you getting value in Request.QueryString["ID"] ?

try directly like this

Session["TestID"] = Request.QueryString["ID"];

Did you debug your code and if statement was executed
if (((Request.QueryString["ID"]) != ""))
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

cougarsyAuthor Commented:
Hello and thank you for your response,

I added your code and the Response.Write executes correctly.  However, when I use this code in the other ASPX page I get an error:  System.NullReferenceException: Object reference not set to an instance of an object.


int thisID = (int)(Session["TestID"]);

Thanks again if you have any ideas.

cougarsyAuthor Commented:
I would like to add that I changed my code to:

Session["TestID"] = Request.QueryString["ID"];

as pratima_mcs suggested and the Response.Write does execute correctly.

But, the problem now is I cannot seem to retrieve the Session variable.  See the code above that throws the error.

Thanks again,

cougarsyAuthor Commented:
One more thing--if I comment out the code above and assign the variable an integer, the code executes correctly:

// int thisID = (int)(Session["TestID"]);
int thisID=167;

So, I am not sure what I am doing wrong.

Many thanks,
Try to use: Convert.ToInt32(Session["TestID"]);
SAMIR BHOGAYTAFreelancer and IT ConsultantCommented:
Hello, You had already write querystring then why converted into int.parse
 EnableSessionState="False"  This property in your page directive.

If it is set to False then set it True
cougarsyAuthor Commented:
Hello and thank you for all of your comments.  The solution was found through using "Convert.ToInt32" in combination with Session["TestID"] = Request.QueryString["ID"];,

So, I will split the points between the two.

Thank you again!


Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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