Category Archives: Csharp

How to get notified via email on error in your application

I am presenting here a simple but useful way to get notified via email, when ever an error is happening in your application with out using try catch. I am creating a httpmodule in app_code for notifying errors. Here is the code for the module.

I am using my “EmailSender” utility for sending emails here. You can find that here. Using this method, first you should create a folder “MailTemplates” in your root folder and add a html template file for “ErrorNotification.htm” in that folder. You can get the referance for creating the html email template from here. Now add the following code in EmailSender class

You are almost done. Now we need to add the module to our application’s configuration file. Add the following configuration value to your web.config, inside system.web and system.webserver (for IIS7).

Hurrey, so it is done. Cheers and Happy coding.

How to load a csv file to a data table

Here is a simple and optimized method to load a csv file to a datatable object. I have gone through many situations when we can not store data in a database, instead have to store in an xml or csv file. This method helped managing those situations effectively. Here is the code for accomplishing this.

Here is how we are going to use it

How to create a simple captcha validation in

We often see captcha validation in number of sites outthere. Earlier, when I started surfing in web, I wondered how they are created. But now I have answer for that.I am presenting you before, a simple method to implement a captcha validation in your websites (PHP folks, cheer up. I am working on a similiar thing in php too.). In this method I am using System.Drawing namespace in C#. First I am going to create a generic handler which will render the captcha image.Look at the code below

And now we are going to implement the validation. Create an aspx page, add add an img tag and for the src attribute,give the path to the handler. See below.

And now it’s time for testing the validation. Code behind will look like this.

And we are done! The validation form will look like this. Download sample project Try this and let me know the feedback.Cheers

How to use Compression method in to speed up website

Common questions related to performance issues of websites

  1. How to improve website performance ?
  2. How to speed up website ?
  3. How to compress webpages
  4. What are the methods to improve page loading speed
  5. How to optimize webpages

I googled alot to find some good and easy method to speed up my websites, already i have optimised all my css and scripts and ofcourse images. So now i need to compress the webpages. And i come up with this solution. It is very simple. This method uses gzip/deflate encoding to compress the webpages. First, create a global.asax file. Now add the following code into that file.

Now upload the file to root of your website.And you are done. But before doing this ensure that you have optimized your css, js and images also. Only then you can feel the change


How to convert normal date time to relative

How to make a normal date like “12/05/2011” to “Some days some hours and some seconds ago”
Here is the answer. We have seen dates in vairous formats, some of you may noticed in facebook or orkut, how they show times. They show relative date and time.
Here is the code for doing it. It is an extension to DateTime Object.

And you are done!. Now you can use the code like this