Sunday, July 26, 2009

Extend your existing classes with extension method in asp.net 3.5

In asp.net 3.5 there is one good features called extension method now you can extend your functionality without modifying existing classes. Extension method allow developers to add own functionality to any existing classes. You don't need to create subclass or don't need to recompile existing classes and still you can extend that class with extension methods. Let's create an example to extend existing string classes to convert a simple string to bold html string.
public static class MyExtensions
{
public static string ConvertToBold(this string mystring)
{
System.Text.StringBuilder myBoldString =new System.Text.StringBuilder(string.Empty);
myBoldString.Append("<strong>");
myBoldString.Append(mystring);
myBoldString.Append("</strong>");
return myBoldString.ToString();  
}
}
So now our extension method is ready. Following the sample code to use this extension method.
protected void Page_Load(object sender, EventArgs e)
{
string helloWorld = "Hello World";
Response.Write(helloWorld.ConvertToBold());
}
While running application you can out like below.
Extension 
Share:

What's new in sql server 2008

Microsoft SQL Server are one of the popular RDBMS(Relational Database Management System) all over world. Before some time Microsoft has launched the new version of SQL Server 2008. SQL Server 2008 provides the highest levels of security, reliability, and scalability for your business-critical applications. . Following are the some of new features of SQL Server 2008.

  1. Policy based management.
  2. Performance Data Collection.
  3. Data compression.
  4. Resource Generator.
  5. Transparent Data Encryption.
  6. External Key Management/Extensible Key Management.
  7. Data Auditing.
  8. Hot-Add CPU and Hot-Add Memory.
  9. Streamlined Installation.
  10. Server Group Management.
  11. Upgrade Advisor.
  12. Partition aligned indexed views.
  13. Backup Compression.
  14. Extended Events.
  15. Grouping Set.
  16. Merge Operator.
  17. Greater Support for LINQ and Entity Framework.
  18. Change Data Capture.
  19. Table Valued Parameters.
  20. Entity data model for entity framework.
  21. Synchronization Server with ADO.NET.
  22. CLR Improvements.
  23. Conflict detection between peer to peer Replication
  24. Service Broker Priorities and Diagnostics.
  25. Spatial Data with Geography and Geometry data types.
  26. Virtual Earth Integration.
  27. Sparse Column.
  28. Filtered Indexes.
  29. Integrated full text search.
  30. File stream data.
  31. Large user defined types.
  32. Large user defined aggregates.
  33. Date/Time Data Types.
  34. Improved XML Support.
  35. ORDPATH.

and many more features are there. It's great rdbms to use in your future projects. Please visit following link to explore above features in great details.

http://www.microsoft.com/sqlserver/2008/en/us/whats-new.aspx

You can download SQL server 2008 Express database for free from following link.

http://www.microsoft.com/express/sql/download/

Share:

How to remove white spaces between tags and lines of html render by asp.net page

When asp.net page loaded in to browser there are lots of white spaces between tags and in tags that will increase your html kb. For example if your page around 300 kb then it will take 3 second to load on 100 kbps internet connection and in dial up connection it will still take time. So if you want to load your site fast on dial up internet connection then you need to decrease html kb as much you can and removing white spaces from the html will be good idea for that.

Following is the code for the page on which you want to remove white spaces from the html. It will decrease your html kb by 30 percentage.

private static readonly Regex REGEX_FOR_TAGS = new Regex(@">s+<", RegexOptions.Compiled);

private static readonly Regex REGEX_FOR_BREAKS = new Regex(@">[\s]*<",
RegexOptions.Compiled);
protected override void Render(HtmlTextWriter writer)
{
using (HtmlTextWriter writer = new HtmlTextWriter(new System.IO.StringWriter()))
{
base.Render(writer);
string htmlkb = writer.InnerWriter.ToString();
htmlkb = REGEX_FOR_TAGS.Replace(htmlkb, "> <");
htmlkb= REGEX_FOR_BREAKS .Replace(htmlkb, "><");
writer.Write(html.Trim());
}
}

Here in the above code there are two regular expression one for white space in tag and another for whitespace between tag and by just overriding the render event of event you can replace the whitespace.

Happy Coding...

Technorati Tags: ,,,

Share:

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews