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,
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;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Did you debug your code and if statement was executed
if (((Request.QueryString["ID "]) != ""))
if (((Request.QueryString["ID
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.NullReferenceExcept ion: Object reference not set to an instance of an object.
Code:
int thisID = (int)(Session["TestID"]);
Thanks again if you have any ideas.
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.NullReferenceExcept
Code:
int thisID = (int)(Session["TestID"]);
Thanks again if you have any ideas.
ASKER
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,
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,
ASKER
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,
// int thisID = (int)(Session["TestID"]);
int thisID=167;
So, I am not sure what I am doing wrong.
Many thanks,
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
EnableSessionState="False"
If it is set to False then set it True
ASKER
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,
So, I will split the points between the two.
Thank you again!
Cheers,
by default asp.net creates a cookie on the browser of client computer to keep track of the session.