29 December, 2009

Difference between Server.Transfer Vs. Response.Redirect

Redirect and Transfer both cause a new page to be processed, but the interaction between the client (web browser) and server (ASP.NET) is different for each situation.
Response.Redirect:
• First sends the request for the new page to the browser then browser sends the request for the new page to
the webserver then after your page changes.
Syntax: Response.Redirect (“Default1.aspx”);
Or
Response.Redirect (“www.google.co.in”);
• The biggest problem is that this method causes each page to be treated as a separate transaction. Besides

making it difficult to maintain your transactional integrity.
• You can’t directly access the values, controls and properties of the previous page with Response.Redirect.
Example. Suppose you are currently on the Page1.aspx and now you are transferring the user to the Page2.aspx using Response.Redirect then When the Page2 page is requested, Page1 has been flushed from the server’s memory and no information can be retrieved about it unless the developer explicitly saved the information using some technique like session, cookie, application, cache etc. But in case of Server.Transfer variables can stay in scope and Page2 can read properties directly from Page1 because it’s still in memory, as you know the Server.Transfer just changes the focus from page1 to page2 So in this case browser doesn’t know that any change is happen there that’s why with this method you can access the information about the previous page.
• Changes the URL in the browser’s address bar.
• Response.Redirect can be used for both .aspx and html pages
• Ex. Response.Redirect(“mypage.html”) OR Response.Redirect(“OtherPage.aspx”)
• Response.Redirect can be used to redirect a user to an external websites.
• There is a new Method called RedirectPermanent () on the HttpResponse Object. This method acts similar to

Response.Redirect () except that the HttpResponse Code it sends out is 301 which indicates that the Page or
Resource has moved permanently. Response.Redirect () returns Response Code 302, which indicates a
temporary redirect.Response.RedirectParmanent () is an extension function introduced in .NET 4.0.

Server.Transfer:
• Instead of telling the browser to redirect, it simply changes the "focus" on the Web server and transfers the
request .This means you don't get quite as many HTTP requests coming through, which therefore eases the
pressure on your Web server and makes your applications run faster.
• The "transfer" process can work on only those sites running on the server, you can't use Server.Transfer to

send the user to an external site.
• Server.Transfer retains the original URL in the browser’s address bar. It just replaces the contents of the

previous page with the new one.
• The Server.Transfer method also has a second parameter—"preserveForm". If you set this to True, using a

statement such as Server.Transfer ("WebForm2.aspx", True), the existing query string and any form
variables will still be available to the page you are transferring to.
• For example, if your WebForm1.aspx has a TextBox control called TextBox1 and you transferred to

WebForm2.aspx with the preserveForm parameter set to True** , you'd be able to retrieve the value of the
original page TextBox control by referencing Request.Form ("TextBox1").
**This has a bug whereby, in certain situations, an error will occur,

The unofficial solution is to set the enableViewStateMac property to “True” on the page you'll be
transferring to, then set it back to “False”. This records that you want a definitive False value for this
property and resolves the bug.
• You can directly access the values, controls and properties of the previous page
• Server.Transfer can be used only for .aspx pages and is specific to ASP and ASP.NET.
• Server.Transfer(“mypage.asp”) OR Server.Transfer(“OtherPage.aspx”)
• Server.Transfer can be used only on sites running on the same server. You cannot use Server.Transfer to

redirect the user to a page running on a different server.
• Mostly the Server.Transfer method is preferable to use because Server.Transfer is faster since there is one

less roundtrip
• Server.Transfer is not recommended since the operations typically flow through several different pages due

to which you lose the correct URL of the page.

26 December, 2009

What is Encapsulation and a simple Example

Encapsulation hides the internal state and behavior of an object. Encapsulation if used with access modifiers such as private, public, protected. It provides a way to protect data.

Example:
public class MyClass
{
private string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
}
public class main
{
public static int Main(string[] args)
{
MyClass myclass = new MyClass();
myclass.name = "Communication";
Console.WriteLine("The name is :{0}", myclass.Name);
return 0;
}
}

17 December, 2009

First C# Console application

Starting with the basic program i.e displaying a message using console application.
The below mentioned program has
1) namespace : e.g. using System;
2) a class : e.g. Class Consoledemo{… }
3) a Main method : e.g. static void Main() {….}
4) statement : e.g. console.writeline(“This is my first article for my site”);

How a complete program in c# look:
// Namespace Declaration
using System;

// Program start class
class Consoledemo
{
// Main begins program execution.
static void Main()
{
// Write to console
Console.WriteLine("This is my first article for my site");
}
}


How to compile a c# code through command line:

CSC.exe Consoledemo.cs