The following example describes the difference.
int i =0;
MessageBox.Show(i.ToString());
MessageBox.Show(Convert.ToString(i));
We can convert the integer “i” using “i.ToString()” or “Convert.ToString”.
The basic difference between “i.ToString()” and “Convert.ToString” is “Convert” function handles NULLS while “i.ToString()” does not and it will throw a NULL reference exception error. Hence the good coding practice is using “convert” which is always safe.
Sunday, August 16, 2009
Difference between Convert.ToString and .ToString()
Labels: .net
Caching in ASP.NET
ASP.NET provides a feature called Caching that improves the performance of web applications by minimizing the usage of server resources . It also reduces the database hits and thereby renders the page faster. There are different types of caching. They are
* Page Output Caching
* Page Fragment Caching
* Data Caching
Page Output Caching
Using this concept the page is cached using an Output Cache engine and all subsequent requests are served from the cache. If whenever a new request comes, this would check if there is a corresponding cache entry for this page. If it finds a corresponding cache entry for the page, the page is rendered from the cache, if not the page being requested is rendered dynamically i.e, the page takes a round trip.
For static Pages this technique will be useful.
Page output caching can be implemented in either of the following two ways:
* At design time using the OutputCache directive
* At runtime using the HttpPolicy class
Following is the syntax of page output caching directive
<%@ OutputCache Duration="no of seconds" Location="Any | Client | Downstream | Server | None" VaryByControl="control" VaryByCustom="browser |customstring" VaryByHeader="headers" VaryByParam="parameter" %>
Following statement is used to implement output caching in an aspx page at design time. The directive is placed at the top of the .aspx page.
<%@OutputCache Duration="30" VaryByParam="none" %>
In the example above the Duration parameter specifies for how long the page would be in cache and the VaryByParam parameter is used to cache different views of the page. Cache duration is a required field, and must be set to an integer greater than zero. The VaryByParam parameter, which is also required, specifies whether the cached page would differ in versions based on any parameter. A value of * in the same parameter indicates that the page would be cached based on all the Get/Post parameters. We can also specify one or more Get/Post parameter(s). Hence, the same statement shown above can have the following variations. The VaryByParam parameter is particularly useful in situations where we require caching a page based on certain criteria. As an example, we might require to cache a specific page based on the CustomerID.
<%@OutputCache Duration="30" VaryByParam="*" Location = "Any"%>
<%@OutputCache Duration="30" VaryByParam="CustomerID" Location = "Client" %>
<%@OutputCache Duration="30" VaryByParam="CustomerID";Basic" %>
The VaryByParam parameter can also have multiple parameters as shown in the example above.
The location parameter is used to specify the cache location, either the server of the client.
To set a page's cacheability programmatically we have to use the method Response.Cache.SetCacheability. This method accepts the following parameters
* NoCache
* Private
* Public
* Server
The code snippet below shows how to set a page’s cacheability programmatically:
Response.Cache.SetCacheability(HttpCacheability.Server);
Additionally we can set other properties which map to the same fields available when using the OutputCache directive on the page:
Response.Cache.SetExpires(DateTime.Now.AddSeconds(30));
Response.Cache.SetValidUntilExpires(true);
Response.Cache.VaryByParams["ID"]= true;
Page Fragment Caching
If you wish to cache a portion of the page then use this method than caching the whole page. This is useful in situations where we can have a page that contains both static and dynamic content.
<%@ OutputCache Duration="15" VaryByControl="EmpID;DeptID" VaryByParam="*"%>
The above code should be placed at the top of any User Control (.axcx file).
Data Caching
The following method checks the Cache whether the data is present in the Cache. If the data is present in the Cache then it fills the Dataset from he Cache else it fills the DataSet from the database and then populates Cache.
public DataSet GetBooks()
{
string cacheKey = "BooksInfo";
DataSet ds = Cache[cacheKey] as DataSet;
if (ds == null)
{
ds = SelectBooks(); // This method fills the DataSet from the Database.
Cache.Insert(cacheKey, ds, null, NoAbsoluteExpiration,
TimeSpan.FromHours(15),CacheItemPriority.High, null);
}
return ds;
}
Cache Expirations
The Cache expirations can be of the following types
* Time Based Expiration
* Dependency Based Expiration
Time Based Expiration
This is used to specify a specific period of time for which the page would remain in the cache. The following statement specifies such expiration for a page in the code behind of a file using C#.
Response.Cache.SetExpires(DateTime.Now.AddSeconds(120));
It can also be accomplished by stating the same in the output cache directive as shown here.
<%@OutputCache Duration="60" VaryByParam="None" %>
Labels: .net