Query String vs. Session Variable

I need to pass certain key information (lets call it Customer Number) from one page to another. Which of the following methods is better and why?

  1. Passing Customer Number in the Query String
  2. Passing Customer Number in a Session Variable
  3. Another method that I am unfamilar with?



1/23/2008 2:46:39 PM
asp.net.getting-started 91979 articles. 4 followers. Follow

7 Replies

Similar Articles

[PageSpeed] 23
Get it on Google Play
Get it on Apple App Store

 You can use hidden variables.

You can also use context variable with Server.Transfer method for moving one page to another.

Session are fine but above 2 are better. 

Shah Dharnendra G
Sr.Analyst Programmer,
1/23/2008 2:55:08 PM

As far as I know there is no "right" answer to your question but I do have opinions on a few methods:

  1. Querystring:  This is good as it only resides as part of the request and not in RAM (or other session state mechanism) but also makes the potentially sensitive data clear to users.  While your application should check authorization whenever data is displayed it does still make the number obvious.  You can encrypt the value which helps.  In general I use this method a lot.
  2. Session:  This is OK if it the value is to be used frequently on multiple pages but then tends to load down the server RAM (or other session state holder) in high usage systems.  In general this is not a preferred method for passing a value to just the next page.
  3. Cross-Page posting:  You can have the form on one page post to the code-behind of another, with all form values along for the ride.  In some situations this is a good method, particularily when there are a lot of variables.  This was used frequently before the .NET days and is coming back into favor again now.

This is all just opinion but I hope it helps.  I don't know your situation but would probably recommend querystrings.

John Stockton
RIA Developer at Ascentium
Co-Author of Silverlight 2 in Action

1/23/2008 2:56:18 PM

We put the customer ID or an Integer that would probably be used site wide in a session object.

1) it is lightweight and so scalability isnt a concern here as opposed to serializing an entire order etc.

2) secure as in your customer ID isnt exposed as readable (Yes you can hash figures on the querystring)

3) if it is in session you can call on it from any page without the need for passing it from page to page on the querystring.

Aside from that I agree it does depend on the scenario.

Just some things to consider!


Please mark the most helpful reply/replies as "Answer".
1/23/2008 3:04:23 PM

 It depend upon the situation if u need the values on the next page and don't want to use them on any other page then only you use the Query String which is very light weight and if you need that information on some other pages then you store value into the session because this value will be available to you through out the website.

Muhammad Usman
Please remember to click "Mark as Answer" on this post if it helped you.
1/23/2008 3:22:01 PM

Thanks for a very clear answer.  I am going to try to learn more about encrypting the query string as well as cross page posting. Both sound like they could do the trick.

1/23/2008 7:37:38 PM

If your value is not in your form, and you dont want to use Querystring or session, you could always expose the value as part of your page class (set up a public readonly property), use Server.Transfer to transfer to your new page, and retreive the value with Context.Handler ie:

page1.aspx (in a page with a Button1 object that can be clicked) 

Private foo As String

Public Readonly Property getfoo() As String
    Return foo
  End Get
End Property

'Switch to new page
Private Sub Button1_Click(ByVal sender as object, ByVal e as System.EventArgs) Handles Button1.Click
End Sub

Then include this in your page2.aspx's Page_Load 

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  Dim bar As String = ctype(context.handler,page1).getfoo
End Sub
2/8/2008 7:27:58 PM

Thanks. That looks like an interesting way to pass the data from page to page.

2/8/2008 8:57:43 PM

Similar Artilces:

Getting Started Without VS.NET
Hi all, does anyone know how any online resources that can show me how to create web apps that include code-behind, and uer controls without using VS.NET? I have VS.NET in work, but would also like to do work at home, but cannot install VS.NET on my home laptop because I simply dont' have the space or processor power. Any online articles would be welcome, as will any open-source apps - I currently have SharpDevelop, but I find it unstable as it crashes my laptop alot of the time. Thanks Tryst Did you try Web Martix? It is a FREE tool from Microsoft.Regards, Narayana ...

Query string and session variables lost after redirect
Hi, I'm trying to redirect a user form one application to another by using response.redirect(). I need to pass a returnURL so I'm doing this through a query string: In myapp1 I call: Response.redirect(http://localhost/myapp2/logon.aspx?ReturnUrl=http://www.mysite.com, true) When debugging the myapp2, in page_load of logon.aspx.cs I've lost the query string AND some session variables I stored. The session ID is the same.... Any idea? Any configuration to be set in web.config? Thanks a lot, Claudio...

Getting encrypted query string parameter value into a variable.
 Hi,   http://localhost/isolutions_source/Startup/RequisitionRecruitment/Forms/OfferLetterAcceptReject.aspx?erSBUi2Lo4usDg6iQAYEjG9f6qseNyxzpYmuhgxKwwBmkoqM8tptWJ+Ro5NivYoluZOz5Z8Dktj+MqEAqV1+Ko2dM3l/djKT   In the above link i am getting a encrypted paramenter value.. Now i want that decrypt that parameter,so that i can use that in my new form.... So plz help by giving me some solution.. Thanks in advance.        You must know the Encryption algorithm. Then and then you can Decrypt this String. - Please check your URL creator code ... - just try...

Session Variables Vs Session Class
Hi All, I am rewriting a classic ASP application in .NET using ASP.net and C#. We used Session Variables to handle storing the current user information throughout the original application.  I was wondering what the benefits would be of using a Session Class over Session Variables, and also how I could implement this? Many thanks in advance for any information you can give me. Regards, Aj Check this out. I think Session class always beneficial since you have more control over accessing and setting your session variables and exception handling. Check the link for its sample implem...

Session variables vs. Session States
What is the difference and in what situations are they used. Thanks, Mattypee A session state (the state of your session) holds your session variables. If your session state expires (due to inactivity) then you lose your session variables...and any other info stored for that session. If you need to store USER specific information within a web app, then session variables are one(of several) ways to do it. MajorCatsMajorCats Is there any code that you could show that would give an example? Thanks, Mattypee example of what? reading and writing session variables? ...

Cache Variable vs. Session Variable
I'm anxious to get some feedback with regard to using a cache variable in lieu of a session variable. My research tells me that caching is the way to go for maintaining state on datasets. However, I've only seen the cache variable discussed as an alternative to an application variable (as opposed to the session variable). I'm actually exploring the idea using of using a cache variable as an alternative to a session variable for maintaining state on a dataset specific to each user (Each user is able to select the fields they wish to view; the user will then be able to page and sort on th...

Passing variables vs session variable
what is the advantage of passing the variable in the url vs storing the variable in a session variable and retrieving it in the second webpage you are accessing?  The variable that you will pass using the QueryString will be visible to the user hence not secured. The value that you put in the Session is not available to the user as it is on the server side and hence more secured. HighOnCodingWanna get high! performance wise does it matter? Just my 2 cents... I remember in college the professors would always harp upon using the Session responsibly. Most of the professors were old...

Getting 3.1 to start in debug mode in vs.net 2003.
  Hi All, I have a run time version of dnn installed and working at D:\Inetpub\wwwroot\dotnetnuke on my laptop. I want to be able to learn how to write DNN modules etc and so I have installed the source code in D:\Inetpub\wwwroot\dotnetnuke3100\. I set the debug flag in web.config to yes..... as in the lines.. <!-- set debugmode to false for running application --> <compilation debug="true" /> I updated the solution file D:\Inetpub\wwwroot\dotnetnuke3100\Solutions\DotNetNuke.All to look at localhost/dotnetnuke3100 which is my source code version. I have created the...

VS.NET 2005 vs VS.NET 2003
I have used VS.NET 2003 extensively and exploited all features like drag and drop functions for creating components, drag and drop data access components...For example VS.NET 2003 allows creating sqldataadapter for table mapping , stored proc parameters mappings etc..We can drag pretty much any object on component designer....Not it seems like all that gone...VS.NET 2005, there is no way we can drag the typed dataset, sqldata adapter, sqlcommand on forms designer . There is no concept of component. There is no component designer any more....Biggest flaw i found is sqldatasource and...

Variables VS Session + Multiple Session Timeouts
I've read that "http is stateless" which is one reason people use Session variables instead of code behind variables. I'm having problems with session variables vanishing after about 10mins. I've checked "App Options" under "Application Configuration" under the properties of the Virtual Directory (IIS 5 on Win2000).  It sais "Session Timeout = 59mins"  web.config <authentication mode="Forms"> <forms timeout="60" /> </authentication>  <sessionState timeout="58" />   Im using master pages, and have declared public variable and session variab...

How to get session variable value into a static variable
Hi, On page preinit i am adding few session variables. Now i want to retrive these session variable values into my static variables. protected void Page_PreInit(object sender, EventArgs e) {Session.Add("T", "Default"); Session.Add("T1", "Red"); Session.Add("T2", "Blue");   }   i have few static variables which shud contain these vaules.  public string TT; public string TT1; public string TextForeColor =sess = "#FFFFFF";   i want to get T value into TT ... T1 into TT1..and so on &n...

vb.net vs c#.net vs j#.net
just out of curiousity which looks, acts most like java? i'm still learning trying to used to vb.net but it seems that it was easier to do some stuff with java. C# is Java++ in my opinion. If you have done C or Jave you will prefer C#.Mike Schellenberger - MCAD If you are looking for language similar to java go for J#. However most code samples you find on the net will be in VB.NET and C#....

VS.NET 2005 vs VS.NET 2003 #2
why there is no datagrid anymore when i drag the tables in design view? it's the gridview,and where is the data adapter. How will i connect the tables now? Here are a couple videos on the new Data model in VS2005.  I think you will find it much easier to use.  I have watch the first one, but the second is by Fritz Onion.  I think there is an Advanced Data Binding Video in the same series done by him as well. ASP.NET HOW DO I Video Series: Data MSDN Webcast: Essential ASP.NET for the Web Developer (Part 5 of 15): Introduction to Data Binding (Level 200) Hope this helpsTh...

How to assign a query string variable to a variable withing a form
I am trying to setup a hosted Satisfaction Survey on our intranet. I am linking to the aspx Satisfaction Survey form with a query string like so: http://<servername>/Survey/survey.aspx?a=WORKITEM(workitem_number) . The WORKITEM(workitem_number) is a macro that pulls the value for 'workitem_number' out of our database. The problem I have is how I can assign the query string value "a" to the value section of this line in the survey : <input id="incident_number" type="hidden" value="WORKITEM(workitem_number)" name="incident_number" />If I type in an actual value in plave of the "WOR...

Web resources about - Query String vs. Session Variable - asp.net.getting-started

Variable - Wikipedia, the free encyclopedia
Text is available under the Creative Commons Attribution-ShareAlike License ;additional terms may apply. By using this site, you agree to the ...

Westpac interest rate rise is one of many variable loan rate rises
WESTPAC is not alone in hitting its owner-occupier customers with a surprise interest rate rise, as dozens of other variable rate home loans ...

Airbnb upgrades variable pricing tool for hosts
Online home rental group Airbnb is rolling out an upgraded variable pricing tool that automatically raises or cuts room rates based on supply ...

Commonwealth Bank raises variable home loan rates
Home owners' mortgage costs are set to rise by hundreds of dollars a year, after the Commonwealth Bank became the latest bank to hike interest ...

CBA lifts variable home loan rates to offset costs associated with stricter capital requirements
The Commonwealth Bank to lift its home loan interest rates independently of the Reserve Bank to partially offset costs associated with stricter ...

Washington Square Signs Spielbergs, Choi Joins Variable and More
... films and an episode of the HBO comedy series "Girls," on which he plays the role of Ray Ploshansky. New York-based creative collective Variable ...

YouTube v10.32 Tweaks Some Interface Elements And Includes Hints About Virtual Reality Content And Variable ...
Forget Update Wednesday! Monday is the new big day. Not only were we treated to the latest M preview release and the official Marshmallow name, ...

"You have to ignore many variables to think women are paid less than men. California is happy to try." ...
Writes Sarah Ketterer in The Wall Street Journal in "The ‘Wage Gap’ Myth That Won’t Die" (which you can get to without subscribing if you Google ...

Staley: 'A lot of different variables...been a hard year'
... 41° Navigation Home Giants A's Sharks Warriors Kings 49ers Raiders Quakes Insiders More Tickets Shop Watch Staley: 'A lot of different variables...been ...

Expert Reveals the Latest Variable Capacity Technology
Learn about gForce Ultra CRAC equipment, the latest variable capacity technology to lower energy usage and increase reliability.

Resources last updated: 1/18/2016 4:52:38 PM