Objects in this mirror are closer to Microsoft Technologies. DNM objective is to help users on Microsoft technologies by providing Articles, snippets, Interview Questions.

30 June 2012

GET and POST methods in HTML/ASP.NET forms – difference between GET and POST


In this article we will discuss the form submission methods GET and POST with examples and differences of them.

The Get/Post methods are used to send client information to web server. The methods are specified in inside form element using method attribute.

Syntax:
<form method="get|post">
The method attribute uses either get or post. Default is GET method.

GET method:
This method appends form data to page request URL with key value pairs called as query string.
E.g.: http://www.dotnetmirror.com/home.htm?key1=value1&key2=value2
  • Page request URL and the form data information (query string) are separated by the ? Character.
  • Get method is restricted to send up to 1024 characters only.
  • Never use Get method to send sensitive information like password, login information to web server.
  • Form data support only ASCII characters.Cannot used to send binary information like word document, text file and image data to server.
  • We can access query string in ASP.NET using Request.QueryString["key1"]
 Example: Usage of Get method In ASP.NET:

In the below example we will see how GET method passed data from login.html (client) to login.aspx page (server).

Login.html
<html>
<body>
<form method="get" action="Login.aspx">
   User Name: <input id="txtuserName" type="text" name="username" />       
    <input id="btnSubmit" type="submit" value="Submit data using GET" />
    </form>
</body>
</html>
Login.aspx
<html>
<body>
    <form id="form1" runat="server">
    <div>
    Welcome <b><% Response.Write(Request.QueryString["username"].ToString()); %></b>
    </div>
    </form>
</body>
</html>
Output:
Login.html
Fig1 - Login.html with GET








Login.aspx
Fig2 - Login.aspx with GET



Explanation: Login.html has username data which is sent using get method to login.aspx (server). In the output of second screen shot we can see there is key (username) value(DotNetMirror) data as query stirng in the URL which is submitted to login.aspx page and the output(Welcome DotNetMirror) is show in login.aspx page.

Post Method:
  • POST method transfers information over HTTP headers.
  • Data transfer through HTTP headers so using secure HTTP protocol we can make sure that data is secured.
  • No restriction on sending data size via Post and also we can send binary data or ASCII information using POST method.
  • We can access form data in ASP.NET using Request.Form["key1"]
 Example: Usage of Post method In ASP.NET:

Login.html
<html>
<body>
<form method="post" action="Login.aspx">
   User Name: <input id="txtuserName" type="text" name="username" />       
    <input id="btnSubmit" type="submit" value="Submit data using POST" />
    </form>
</body>
</html>
Login.aspx
<html>
<body>
    <form id="form1" runat="server">
    <div>
    Welcome <b><% Response.Write(Request.Form["username"].ToString()); %></b>
    </div>
    </form>
</body>
</html>
Output:
Login.html
Fig3 - Login.html with POST








Login.aspx
Fig4 - Login.aspx with POST


Explanation: Login.html has username data which is sent using post method to login.aspx (server).The output (Welcome DotNetMirror) is show in login.aspx page.

Note: we can observe the post method did not send any query string like what we have seen in get method. Fig2 has query string where Fig4 does not have data in URL.

Usage of Post method:
  • If the form data is large then use POST method because GET method cannot handle long URL’s
  • Form data contains Non-ASCII characters use POST because GET doesn’t support it.

Difference between GET and Post:

GET
POST
Data will be arranged in HTTP header by appending to the URL as query string
Data will be arranged in HTTP message body.
Data is in query string so user can view the data
Not visible to user
Less secured compared to POST method because data is in query string so it will be saved in browser history and web server logs
Bit safer than GET method because data is not saved in history or web server logs
As data is saved in URL so its saves only 2048 bytes data
Can be used for any amount of data
Can be bookmarked
Can’t bookmarked
Hacking will be easy
Hacking is difficult
Only ASCII character data type allowed
No restrictions. Allows binary data also
Caching is possible
No caching


29 June 2012

Replace function in SQL Server with examples


In this article we will discuss the definition and syntax of replace function along with examples like simple usage of replace, string found multiple times, no string found, passing INT data as string to replace, replace with update statement and where condition, replacing single quote with double quote and using of nested replace function.

"REPLACE is a SQL server function which replaces the specified string with another string."

Syntax:
REPLACE(string1,string2,string3)
string1 - string to be searched
string2 - string to be found
string3 - string to be replaced.
string1, string2 or string3 can be of type character or binary data type.

Return Type:
·         nvarchar -if one of the input arguments is of the nvarchar data type
·         varchar – argument datatype as other than nvarchar data type
·         NULL - if any one of the arguments is NULL.

Let’s understand with simple example
SELECT REPLACE('DotNetMirror','dot','.')
Output - .NetMirror
Explanation: The searched string “dot” is found once in  'DotNetMirror’ so it will replace dot string with . and returns “.NetMirror ”

String found multiple times:

If string2 found multiple times in string1 then it will replaces all the occurrences which are matched with string3 in string1.

SELECT REPLACE('DotNetMirror DotNetMirror','dot','.')
Output - .NetMirror .NetMirror
Explanation: The searched string “dot” is found 2 times in  'DotNetMirror DotNetMirror' so it will replace dot string with . and returns “.NetMirror .NetMirror”

No String found:

If none of string found in string1 then it will return the orginal string i.e. string1 

SELECT REPLACE('DotNetMirror','reflects','.')
Output: -  DotNetMirror
Explanation: Because “reflects”string is not found in DotNetMirror so it will not replace any content with .(dot) and returns original value

 Passing INT value to be searched in string:

We can pass INT values in string2 and string3 that will also replaces if the string matches. 

SELECT REPLACE('DotNetMirror 456','456','123')
Output: -  DotNetMirror 123
Explanation:  The searched string “456” is found once in  'DotNetMirror 456’ so it will replace 456 string with 123 and returns DotNetMirror 123”

Replace with update statement

In some scenarios we need to replace a string in a table column data so we can use below query.

UPDATE Student SET FirstName=REPLACE(FirstName, 'Kumar', 'Kumar sir')
Output: - replaces first name column data with ‘kumar ji’ if any of string contrains kumar in column data.

Replace with update statement and where condition:

The above statement replaces all the matched occurrences in the column data in order to update for particular columns we can replace function in update statement with where condition.
E.g :
UPDATE Student SET FirstName=replace(FirstName, 'Kumar', 'Kumar sir') WHERE Country='India'
Output: - replaces first name column data with ‘kumar ji’ if any of string contrains kumar in FirstName column data for students those belongs to country India.

Replace single-quote (') with double-quote ("):

In order to get a single quote in a string we need to give two single quotes.
select replace('''DotNETMirror''', '''', '"')
Output: "DotNETMirror"

Using nested replace:

select replace(REPLACE('DotNetMirror','dot','.'),'.','Dot')
Output: DotNetMirror
Explanation: From inner replace method dot is replaced with .(dot)  which returns .NetMirror and again from outer replace function .(dot) is replaced with Dot so it returns the original value “DotNetMirror”