Difference between Query string and Cookies

Posted by Krishnamanohar under ASP.NET on 1/30/2013 | Points: 10 | Views : 8156 | Status : [Member] | Replies : 6
Hi Guys,
can anyone please tel me the exact difference between Qurery string and Cookie.
which one is the best one to pass data?
in which senario we go for querystring and which senario we go for cookies.'
---------------------------------
Thanks in advace
Krish




Responses

Posted by: Shanky11 on: 1/30/2013 [Member] Starter | Points: 25

Up
0
Down
cookies is a text file stored on client machine when we surf ant thing on internet by the server automatically we dont have to create it

query string is used to transfer data from 1 page to anothe but this is not safe s it shows in url what data we r sending
pen any site and see url after question mark tht is url
if u understood then mark this as correct ans if don't tell me what r u nt understanding ???????????????????

Krishnamanohar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kusumas on: 1/30/2013 [Member] Starter | Points: 25

Up
0
Down
http://www.wiseowl.co.uk/blog/s319/asp-net-state.htm


Krishnamanohar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Umar on: 1/31/2013 [Member] Starter | Points: 25

Up
0
Down
Query String

Query string is the limited way to pass information to the web server while Transferring from one page to another page. This information is passed in url of the request. see below the code sample


Code Sample

//Retrieving values from query string
String name;
//Retrieving from query string
name = Request.Param["umar"].ToString();

But remember that many browsers impose a limit of 255 characters in query strings. You need to use HTTP-Get method to post a page to server otherwise query string values will not be available.

Cookies

A cookie is a small amount of data which is either stored at client side in text file or in memory of the client browser session. Cookies are always sent with the request to the web server and information can be retrieved from the cookies at the web server.Cookies can be permanent in nature or temporary. ASP.Net internally stores temporary cookie at the client side for storing session identifier. By default cookies are temporary and permanent cookie can be placed by setting "Expires" property of the cookie object.

Regards,


Umar Deen

Krishnamanohar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 1/31/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Cookies: - Cookies are little pieces of information that a server stores on a browser. They are of two types
1. Temporary cookie
2. Persistent cookie

Temporary cookie: - They are also known as session cookies. These are volatile in nature. When the browser is shutdown they are erased.

Persistent cookie:- These may be called as permanent cookies. These are especially saved in files. It may remain for a month or year.

Properties of cookies
Some properties of cookie
Name: - represent the name of cookie.
Name value: - represent a collection of key values of cookie
Domain: - represent the domain associated with a specific cookie.
Path: - the path associated with a cookie.
Expires: - expired time of cookie.
Hashkey: - identifies whether the cookie is a cookie dictionary.
Secure: - specifies whether the cookie is to be sent in an encrypted connection or not.

Working with cookies
Cookies are send back and forth between a browser and server. The server creates a cookie by using the set cookie header. Subsequent request from the browser is send to the server in cookie header. Suppose the server create a cookie

Set cookie: username=rajkumar; path=1;domain=abc.com; expires=Friday, 19th august 20116:30:00GMT
The server creates the cookie named as username containing the user information. Path identifies any requested page to the website, domain identifies the specific website abc.com, expired identifies that this cookie would be expired on the specific data mentioned.
After the server creates the cookie the response is send to the server from the browser for each subsequent request to the webpage. The reader sends in the format cookie: username=raj+kumar
The server is using set cookie reader. The browser is using reader.

Creation and reading session cookies
The session cookies can be created by using the response object and can be read by using the request object. The cookies properties of the response and request objects contains instances of Httpcookies collection class. Each cookie is created by using Httpcookie class.

For creating cookies
Dim cookies1 As New HttpCookie("raj", "kumar")
Response.Cookies.Add(cookies1)
For reading cookies
Response.Write(request.cookies(cookies1).value)

Program

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
'For creating cookies

Dim cookies1 As New HttpCookie(TextBox1.Text, TextBox2.Text)
Response.Cookies.Add(cookies1)
clear()
End Sub

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
'For reading cookies
Dim s As String
For Each s In Request.Cookies
Label1.Text += "<li>" & s
Next
End Sub

Sub clear()
TextBox1.Text = ""
TextBox2.Text = ""
End Sub
End Class

Persistence cookies
It is saved in a config file.it has a specific expiration date. It is especially used to store information about userid, name of authenticated user.

For creating persistence cookie
Dim pcookie1 as New HttpCookie("user name"."ravi kumar")
Response.Cookies.Add(pcookie1)
pcookie1.expires= #19/06/2011#


For reading persistent cookie
Response.Write(Request.cookies("user name").Value)

Limitation of cookies: -
1.A cookie can store maximum of 4kb of data.
2.A cookie can contain only string values. It can contain objects like dataset object.
3.A cookie is browser dependent.
4.Persistent cookie may be deleted before expiration date by browser.

Advantages
1. Cookies do not require any server resources since they are stored on the client.
2. Cookies are easy to implement.
3. You can configure cookies to expire when the browser session ends (session cookies) or they can exist for a specified length of time on the client computer (persistent cookies).
Disadvantages
1. Users can delete a cookies.
2. Users browser can refuse cookies,so your code has to anticipate that possibility.
3. Cookies exist as plain text on the client machine and they may pose a possible security risk as anyone can open and tamper with cookies



Query string is used to Pass the values or information form one page to another page.
Syntax
Request.QueryString(variable)[(index)|.Count]
Parameters
variable :-
Specifies the name of the variable in the http query string to retrieve.
index :-
An optional parameter that enables you to retrieve one of multiple values for variable. It can be any integer value in the range 1 to Request.QueryString(variable).Count.

For example:-
In page 1:
Drag and drop the one textbox and button control
In page 2:
Drag and drop the one textbox control from tool box and place it on form.
In page one:-


Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
'bind the textbox values to querystring
Response.Redirect("Default2.aspx?Textboxvalue=" & TextBox1.Text)

End Sub
End Class


In page two:-


Partial Class Default2
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Get the values from Query string
TextBox1.Text = Request.QueryString("Textboxvalue")
End Sub
End Class

If this post helps you mark it as answer
Thanks

Krishnamanohar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Saratvaddilli on: 1/31/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Nice explanation satyapriyanayak

Thanks and Regards
V.SaratChand
Show difficulties that how difficult you are

Krishnamanohar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Itishree on: 2/19/2013 [Member] Starter | Points: 25

Up
0
Down
Hi,

Cookies:

Cookies are useful for storing less data of frequently changed information on the client. When server wants the information Cookie keeps tracking of information.
-Cookie can expire when the browser session ends, or it can exist indefinitely on the client computer, It depends on the assigned expiration time of Cookies.
-No server resources are required.
-We can send secure data by encryption and decryption but need more coding.
-Simplicity The cookie is a lightweight, text-based structure with simple key-value pairs.
-Cookies are browser dependent, thereby limiting this functionality.
-Size limitations most browsers place a 4096-byte limit on the size of a cookie, but for few browsers support for 8192-byte.

Query Strings:
-A query string is information that is passed through the URL of a page.Query string can be used to submit data back to your page or to another page through the URL.
-No server resources are required the query string is contained in the HTTP request for a specific URL.
-Less code and easy way to pass information to different pages but not secured.
-Almost all browsers and client devices support using query strings to pass values.
-ASP.NET provides full support for the query-string method.
- Limitation for the length of browser URL is 2083-character limit.


Regards,
Itishree

Krishnamanohar, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response