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

Session Variable question for C#

Hello,

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#.

Cheers,  
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

0
cougarsy
Asked:
cougarsy
2 Solutions
 
mayank_joshiCommented:
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.
0
 
Pratima PharandeCommented:
are you getting value in Request.QueryString["ID"] ?

try directly like this

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

Response.write(Session["TestID"]);
0
 
dj_alikCommented:
Did you debug your code and if statement was executed
if (((Request.QueryString["ID"]) != ""))
0
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.

Code:

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

Thanks again if you have any ideas.

0
 
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,

0
 
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,
0
 
dj_alikCommented:
Try to use: Convert.ToInt32(Session["TestID"]);
0
 
SAMIR BHOGAYTAFreelancer and IT ConsultantCommented:
Hello, You had already write querystring then why converted into int.parse
0
 
nidheeshmcaCommented:
Check
 EnableSessionState="False"  This property in your page directive.


If it is set to False then set it True
0
 
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!

Cheers,
0

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