Wednesday, January 30, 2008

Asp.Net Caching terms.... It means proper performance (Partial Page Caching)

Partial Page Caching makes sense when a page contains both dynamic and static contents.
Two methods to apply this concept :
  1. Post-Cache Substitution:
  2. Caching with a User Control :
Let us start with Post-Cache Substitution:
See this example :
Note :
  • Substitution Control will not cached
  • The method must be a static method because an instance of the class is not created when the page is output cached.
  • Remaining part of page will be cached.
  • Post-cache substitution is used internally by the AdRotator control. Even when you use Page Output Caching to cache a page that contains an AdRotator control, the content rendered by the AdRotator control is not cached.

you can use post-cache substitution programmatically by using the Response.WriteSubstitution() method
see this example :

Note:
  • There are two advantages to using the WriteSubstitution() method. First, the method referenced by the WriteSubstitution() method does not have to be a method of the current class. The second is that you can use it within a custom control to perform post-cache substitutions.
Let us start Caching with a User Control:

Using post-cache substitution is appropriate only when working with a string of text or HTML. If you need to perform more complex partial page caching, then you should take advantage of User Controls.

You can cache the rendered contents of a User Control in memory in the same way as you can cache an ASP.NET page. When you add an directive to a User Control, the rendered output of the User Control is cached

Note :
  • When you cache a User Control, the content is cached on the web server and not on any proxy servers or web browsers. When a web browser or proxy server caches a page, it always caches an entire page.
  • Shared attribute Enables you to share the same cached version of the User Control across multiple pages
Caching with a User Control programmatically ,see this example:
Creating a User Control Cache File Dependency:
You can use the CacheControlPolicy.Dependency property to create a dependency between a cached User Control and a file (or set of files) on the file system.

Caching Dynamically Loaded User Controls:
you need to be aware that when a cached User Control is loaded dynamically, the ASP.NET Framework automatically wraps the User Control in an instance of the PartialCachingControl class. Therefore, you need to cast the control returned by the Page.LoadControl() method to an instance of the PartialCachingControl class.
See this example:

Ref :
Code Project WebSite
Unleashed ASP.NET 2.0 book
AppDev Video

Monday, January 28, 2008

Asp.Net Caching terms.... It means proper performance ( Page Output Caching)

Behind the best practices of Coding and Designing for better performance and fast requests we should dealing with Asp.Net Caching.


Actually there are four types of Caching :
  1. Page Output Caching
  2. Partial Caching
  3. DataSource Caching
  4. Data Caching
Let us start with first one


Page Output Caching :
Enable you to cache the entire rendered contents of the whole page in memory (Every thing in ViewSource ) so when the user request the same page next time ,the page is retrieved from the caching memory.

Note :

Fiddler tool enable you to view the row request and response HTTP trafic for testing mode.
How to do:
Note:
  • There is no guarantee that a page will be cached for the amount of time that you specify. When server memory resources become low, items are automatically evicted from the cache
  • the page by default cached in multiple locations :Browser , Proxy server and Web server
  • Duration attribute measures by Seconds
  • You can cache the page based on the parameter (Query Strigng param or Form param) so in above example not required parameter so the value is "none"
In situation of Master /Details page , you pass a query string parameter to indicate the particular Detail to display. Let us see the example :

../PageCachingByParam.aspx?id=12 and

./PageCachingByParam.aspx?id=15]

and separate page content is generated for each of them, the directive should be:



Note:
  • To specify multiple parameters, use semicolon to separate the parameter names. If we specify the VaryByParam attribute as *, the cached content is varied for all parameters passed through the querystring.
You can use VaryByControl attribute to generate diff cached page based on the value of the control ,Let us see the example :

Note:
  • Each time your select from the dropdownlist with diff value ,the entire page will cached.

Note
A better way to create different cached versions of a page that depend on the type of browser being used to request the page is to use the VaryByCustom attribute. This attribute accepts the special value browser (IE 5.0 is diff than IE 6.0 or IE 6.0 is diff than Firefox ...)

By using VaryByCustom ,you can specify a custom method that determines when diff cache version of a page is generated. Let us see the example :
  1. In Global.asax , write this code :
public override string GetVaryByCustomString(HttpContext context, string custom)
{
if (String.Compare(custom, "css") == 0)
{
return Request.Browser.SupportsCss.ToString();
}
return base.GetVaryByCustomString(context, custom);
}

2.create a page as :




You can use the Location attribute of the directive to specify where a page is cached. This attribute accepts the following values:
Any The page is cached on the browser, proxy servers, and web server (the default value).
Client The page is cached only on the browser.
Downstream The page is cached on the browser and any proxy servers, but not the web server.
None The page is not cached.
Server The page is cached on the web server, but not the browser or any proxy servers.
ServerAndClient The page is cached on the browser and web server, but not on any proxy servers.

There are situations in which you might need to modify this default behavior. For example, if you are caching private information, then you don't want to cache the information on the web server or any proxy servers.

Ex :

Creating a Page Output Cache File Dependency:That means when a file is modified the cache page removed from the memory and reloaded again. Let us see this example :
this page displays the contents of an XML file in a GridView




You can remove a page from the cache programmatically by using the Response.RemoveOutputCacheItem() method

Let us assume that we have two pages , the first is List.aspx (to List all products) and the second is AddNew.aspx(to add new product) when adding new product we want to remove the List page from the cache : and Add Event()

HttpResponse.RemoveOutputCacheItem(Page.ResolveUrl("~/MovieList.aspx"));
Response.Redirect("~/MovieList.aspx");
Manipulating the Page Output Cache Programmatically:


Creating Page Output Cache Profiles:



then to apply to a particular page :



Finally, Page Output Cache is fine when the page content is the same for all request.
Ref :
  • Code Project WebSite
  • Unleashed ASP.NET 2.0 book
  • AppDev Video

Tuesday, January 15, 2008

How to Debug Your MS - SQL Server 2005 Stored Procedure ?

Let us start with this Example :


1.Open Visual Studio 2005


2.Select from View menu > Server Explorer then choose

Connect to databas

3. Enter Server name , Authentcation Mode and Select Your Database


4.Browse to select your Stored Procedure as Following :



then Right click > Step Into Stored Procedure
5.Now , Start Debugging with Your Stored Procedure and Have a nice Bugs :)

Monday, January 14, 2008

How to Debug your WebPart In Sharepoint Server 2007 ??

Follow me with this example
  1. Open Visual Studio 2005 to Create New Class Library called "Welcome"
  2. Add Ref... System.Web.dll
  3. Write this Class as Following :


4.Right Click on your project then choose properties > Build Tab > then choose Output Section :

Go to Output path and Browse to your Portal bin Folder ,
ex : C:\Inetpub\wwwroot\wss\VirtualDirectories\24193\bin\

5. then build your class
6.Open web.confg of your portal and make these changes :
debug="true">
and add under Safe Controls Section :

7.Now let us add this webpart to our portal so go to Site Settings>WebParts>New
and check Welcome.Hi webpart to added to your gallery
8.Now go Back to VS 2005 and add BreakPoint as the above picture
9.from Debug menu choose Attach to process.. and choose all w3wp.exe process
10. Return to Homepage of Portal and click at SiteSettings>Edit Page and add Your Webpart (Hi Webpart )
11.Final , You can start with debugging and Have A nice Bugs :)