Link to home
Start Free TrialLog in
Avatar of cougarsy
cougarsy

asked on

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

Avatar of mayank_joshi
mayank_joshi
Flag of India image

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.
ASKER CERTIFIED SOLUTION
Avatar of Pratima
Pratima
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dj_alik
dj_alik

Did you debug your code and if statement was executed
if (((Request.QueryString["ID"]) != ""))
Avatar of cougarsy

ASKER

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.

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,

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,
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello, You had already write querystring then why converted into int.parse
Check
 EnableSessionState="False"  This property in your page directive.


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