Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi All

I want to use values stored in query string and session in my another page.
I tried this but for some reason I am not getting values in the session.

Can anyone plz help me.


page1.aspx.cs

C#
 protected void Page_Load(object sender, EventArgs e)
        {
sNo = (string)Request.QueryString["no"].Trim();
               sYear = (string)Request.QueryString["Year"].Trim();
                Session["Year"] = " ";
                Session["no"] = " ";
}



page2.aspx.cs

C#
protected void Page_Load(object sender, EventArgs e)
        {
 sYear = (string)Session["Year"];
 sNo = (string)Session["no"];
 
}



Thanks
Posted
Updated 23-Sep-13 5:34am
v2
Comments
[no name] 23-Sep-13 11:32am    
Why do you bother creating variables named "sYear" and "sNo" and then not use them? Session["Year"] contains the string "sYear" does it not? What do you mean you are not getting values in your session? What values are you getting? What values do you want to get?
babli3 23-Sep-13 11:37am    
Yes sorry ! I was not clear actaully..

I have one master page from where we will get these query string values and then I have to store these values into session so that these can be accessed in another pages.... thanks
[no name] 23-Sep-13 11:42am    
Okay so go ahead and do that. That does not make anything any clearer.
Richard C Bishop 23-Sep-13 11:32am    
The only thing you are doing is storing a hard coded string value in your session variables.
babli3 23-Sep-13 11:37am    
Yes sorry ! I was not clear actaully..

I have one master page from where we will get these query string values and then I have to store these values into session so that these can be accessed in another pages.... thanks

Try this:
C#
protected void Page_Load(object sender, EventArgs e)
    {
    sNo = (string)Request.QueryString["no"].Trim();
    sYear = (string)Request.QueryString["Year"].Trim();
    Session["Year"] = sYear;
    Session["no"] = sNo;
    }
 
Share this answer
 
Comments
babli3 23-Sep-13 11:39am    
Hi

I want these query string values to be used in my another page....Thanks
OriginalGriff 23-Sep-13 11:50am    
And that is what it will do - it reads the query strings, and saves them to the session - your version saved the names of the variables into the session instead of the actual values from the query string.
The code you have for reading the session values in page two will work.
The only change I would make is to check IsPostback in the page load and only set the Session values if it is not a postback.
babli3 23-Sep-13 11:55am    
ok thank you :)
OriginalGriff 23-Sep-13 12:01pm    
You're welcome!
babli3 24-Sep-13 5:36am    
HiIs this right .... I am assigning the above session values for the different(2nd page) page
sYear = Session["Year"].ToString();
sNo = Session["no"].ToString();

Thanks
The problem is because you are storing the string name instead of the actual value!

C#
protected void Page_Load(object sender, EventArgs e)
        {
sNo = (string)Request.QueryString["no"].Trim();
               sYear = (string)Request.QueryString["Year"].Trim();
                Session["Year"] = "sYear";//This is wrong!
                Session["no"] = "sNo";//This is wrong!
}


Use the following instead:

C#
protected void Page_Load(object sender, EventArgs e)
        {
sNo = (string)Request.QueryString["no"].Trim();
               sYear = (string)Request.QueryString["Year"].Trim();
                Session["Year"] = sYear; //Assign the actual value!
                Session["no"] = sNo;//Assign the actual value!
}
 
Share this answer
 
Comments
babli3 23-Sep-13 11:40am    
Hi

I cannot assign the actual values because the values changes according to the project...
babli3 24-Sep-13 5:36am    
HiIs this right .... I am assigning the above session values for the different(2nd page) page
sYear = Session["Year"].ToString();
sNo = Session["no"].ToString();

Thanks
Abey Thomas 24-Sep-13 5:40am    
should work :)
babli3 24-Sep-13 5:53am    
thank u

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900