<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-2770957963577646170</id><updated>2012-01-20T05:02:21.519-08:00</updated><title type='text'>ASP .NET Programmers Guide</title><subtitle type='html'>It's a collection of Asp.Net articles for developers.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://ravi-testingblog.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2770957963577646170/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://ravi-testingblog.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>ravi</name><uri>http://www.blogger.com/profile/07517056757956161890</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>12</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-2770957963577646170.post-5610086309870841346</id><published>2009-07-28T10:51:00.000-07:00</published><updated>2009-07-28T11:00:37.442-07:00</updated><title type='text'>Using NHibernate and Log4Net in ASP.NET 2.0 applications</title><content type='html'>&lt;h2&gt;Introduction&lt;/h2&gt; &lt;p class="ArticleText"&gt;Hibernate and Log4J are a de-facto standard of ORM (Object-relational mapping) and logging (respectively) in Java world.  That's why both were ported to .NET runtime environment.&lt;/p&gt;&lt;p class="ArticleText"&gt;NHibernate is ORM solution and is intended for transparent binding .NET classes to database tables.  NHibernate was designed to reduce time efforts to switch application to another database provider.  It is achieved by means of replacing SQL with special NHibernate query language (HQL),  providing dialects for several databases and providing special classes for retrieving/updating data in database.&lt;/p&gt; &lt;p class="ArticleText"&gt;Log4Net is logging framework which is useful for tracking errors in application and can write logs either to database table or flat files. You can download sample Visual Studio .NET project &lt;a href="http://www.beansoftware.com/ASP.NET-Tutorials/Examples/NHibernate-Log4Net.zip"&gt;here&lt;/a&gt; &lt;/p&gt; &lt;h2&gt;NHibernate in n-tier application&lt;/h2&gt; &lt;p class="ArticleText"&gt;Speaking about modern approach to web-site development with ASP.NET you should be acquainted with n-tier approach.  Generally n-tier application consists of data access tier, business logic tier, presentation tier and  domain objects which spans across all tiers (this is 3-tier approach which is shown below)&lt;/p&gt; &lt;div align="center"&gt;&lt;p class="ArticleText"&gt;&lt;img src="http://www.beansoftware.com/ASP.NET-Tutorials/Images/NHibernate-Log4Net-3-tier.jpg" /&gt;&lt;br /&gt;Figure 1: A Typical 3-tier Model&lt;/p&gt;&lt;/div&gt; &lt;p class="ArticleText"&gt;In the figure above NHibernate manages Data Access tier which is responsible of loading/updating data into/from domain objects.  Business Logic tier uses Data access objects to access data and perform computations upon data,  while Presentation tier (in our case ASP.NET 2.0 pages)  displays data to end-user and process user input passing it back to Business logic tier and so on.&lt;/p&gt; &lt;h2&gt;Getting started with NHibernate&lt;/h2&gt; &lt;p class="ArticleText"&gt;Now we can try to create a simple ASP.NET 2.0 application using NHibernate objects as Data Access tier.  First of all we should start new ASP.NET project and add  NHibernate binaries to project references (this is done via Add reference option from project menu.  NHibernate binaries can be found at &lt;a href="http://www.hibernate.org/6.html"&gt;http://www.hibernate.org/6.html&lt;/a&gt;.  We need several libraries from NHibernate package.  The best way is to add all .dll files from NHibernate distribution located at bin\net-2.0 folder (in version 1.2).  Among them you will notice log4net library which we will use later.&lt;/p&gt; &lt;p class="ArticleText"&gt;The next thing to do - is to configure our NHibernate to use certain database.  This is done in web.config file for your web site. Here is example of typical content of web.config:&lt;/p&gt; &lt;p class="ArticleText"&gt;&lt;span style="color: blue;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color: maroon;"&gt;xml&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt; version&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;1.0&lt;/span&gt;"&lt;span style="color: blue;"&gt;?&lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;configuration&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt; xmlns&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;http://schemas.microsoft.com/.NetConfiguration/v2.0&lt;/span&gt;"&lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;  &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;configSections&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;section&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt; name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;nhibernate&lt;/span&gt;"&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;type&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;System.Configuration.NameValueSectionHandler,  System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089&lt;/span&gt;"&lt;span style="color: blue;"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;  &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;configSections&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;  &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;system.web&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;    ...&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;  &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;system.web&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;  &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;appSettings&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;    ...&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;  &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;appSettings&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;  &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;nhibernate&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;add&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;key&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;hibernate.show_sql&lt;/span&gt;"&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;true&lt;/span&gt;"&lt;span style="color: blue;"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;add&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;key&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;hibernate.connection.provider&lt;/span&gt;"&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;    &lt;/span&gt;&lt;span style="color: red;"&gt; value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;NHibernate.Connection.DriverConnectionProvider&lt;/span&gt;"&lt;span style="color: blue;"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;add&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;key&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;hibernate.dialect&lt;/span&gt;"&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;NHibernate.Dialect.MsSql2000Dialect&lt;/span&gt;"&lt;span style="color: blue;"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;add&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;key&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;hibernate.connection.driver_class&lt;/span&gt;"&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;NHibernate.Driver.SqlClientDriver&lt;/span&gt;"&lt;span style="color: blue;"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;add&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;key&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;hibernate.connection.connection_string&lt;/span&gt;"&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;Password=;Persist  Security Info=True;User ID=sa;Initial Catalog=test;Data Source=127.0.0.1;&lt;/span&gt;"&lt;span style="color: blue;"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;add&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;key&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;hibernate.cache.use_query_cache&lt;/span&gt;"&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;true&lt;/span&gt;"&lt;span style="color: blue;"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;  &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;nhibernate&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;configuration&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="ArticleText"&gt;The most important part for us in this fragment is connection string  which should be set as a value of  "hibernate.connection.connection_string" key.  It should point to database intended for the test application.&lt;/p&gt; &lt;h2&gt;Going on with session&lt;/h2&gt; &lt;p class="ArticleText"&gt;As in ADO.NET we use database connection class to create queries or data adapter, in NHibernate we use session object which is responsible for maintaining connection to database, loading data into domain objects, detecting changes to domain object and syncing domain state with database. NHibernate requires at least one session to work. As NHibernate caches domain object instances to ensure that single row is loaded only once from database multi-user work with one session object is questionable. This issue differ usage of NHibernate in WinForms and ASP.NET application. The general idea is to have only one session per request and this can be achieved either by using singleton pattern on using HttpModule. We will use the last one. To do this we need to create a custom class, make it inherit IHttpModule interface and add following lines to web.config system.web section:&lt;/p&gt; &lt;p class="ArticleText"&gt;&lt;span style="color: blue;"&gt;  &amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;httpModules&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;add&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt; type&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;Dummy.DAL.NHibernateHttpModule&lt;/span&gt;"&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;NHibernateHttpModule&lt;/span&gt;"&lt;span style="color: blue;"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;  &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;httpModules&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="ArticleText"&gt;NHibernateHttpModule  - the class youâ€™ve created - is fair simple. The main idea is to open session upon beginning of the request and  flush and close session when the request has ended. This class should contain following code:&lt;/p&gt; &lt;p class="ArticleText"&gt;&lt;span style="color: blue;"&gt; public&lt;/span&gt;&lt;span&gt; &lt;span style="color: blue;"&gt;static&lt;/span&gt; &lt;span style="color: blue;"&gt;readonly&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt; KEY = &lt;span style="color: maroon;"&gt;"NHibernateSession"&lt;/span&gt;;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt; private&lt;/span&gt;&lt;span&gt; &lt;span style="color: blue;"&gt;static&lt;/span&gt; ISession _session;&lt;/span&gt;&lt;br /&gt; &lt;span style="font-size: 14pt;"&gt; &lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt; private&lt;/span&gt;&lt;span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; context_BeginRequest(&lt;span style="color: blue;"&gt;object&lt;/span&gt;  sender, &lt;span style="color: teal;"&gt;EventArgs&lt;/span&gt; e)&lt;/span&gt;&lt;br /&gt; &lt;span&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span&gt;       HttpApplication application = (HttpApplication)sender; &lt;/span&gt;&lt;br /&gt; &lt;span&gt;       HttpContext context = application.Context;  &lt;/span&gt;&lt;br /&gt; &lt;span&gt;       context.Items[KEY] = OpenSession(); &lt;/span&gt;&lt;br /&gt; &lt;span&gt;} &lt;/span&gt;&lt;br /&gt; &lt;span&gt; &lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt; private&lt;/span&gt;&lt;span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; context_EndRequest(&lt;span style="color: blue;"&gt;object&lt;/span&gt;  sender, &lt;span style="color: teal;"&gt;EventArgs&lt;/span&gt; e) &lt;/span&gt;&lt;br /&gt; &lt;span&gt;{ &lt;/span&gt;&lt;br /&gt; &lt;span&gt; HttpApplication application = (HttpApplication)sender; &lt;/span&gt;&lt;br /&gt; &lt;span&gt;       HttpContext context = application.Context; &lt;/span&gt;&lt;br /&gt; &lt;span&gt; &lt;/span&gt;&lt;br /&gt; &lt;span&gt;       ISession session = context.Items[KEY] &lt;span style="color: blue;"&gt;as&lt;/span&gt;  ISession;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;      &lt;span style="color: blue;"&gt;if&lt;/span&gt; (session != &lt;span style="color: blue;"&gt;null&lt;/span&gt;) &lt;/span&gt;&lt;br /&gt; &lt;span&gt;      { &lt;/span&gt;&lt;br /&gt; &lt;span&gt;             &lt;span style="color: blue;"&gt;try&lt;/span&gt; &lt;/span&gt;&lt;br /&gt; &lt;span&gt;             { &lt;/span&gt;&lt;br /&gt; &lt;span&gt;                   session.Flush();&lt;/span&gt;&lt;br /&gt; &lt;span&gt;                   session.Close(); &lt;/span&gt;&lt;br /&gt; &lt;span&gt;             } &lt;/span&gt;&lt;br /&gt; &lt;span&gt;             &lt;span style="color: blue;"&gt;catch&lt;/span&gt; {} &lt;/span&gt;&lt;br /&gt; &lt;span&gt;      } &lt;/span&gt;&lt;br /&gt; &lt;span&gt; context.Items[KEY] = &lt;span style="color: blue;"&gt;null&lt;/span&gt;; &lt;/span&gt;&lt;br /&gt; &lt;span&gt;} &lt;/span&gt;&lt;br /&gt; &lt;span&gt; &lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt; public&lt;/span&gt;&lt;span&gt; &lt;span style="color: blue;"&gt;static&lt;/span&gt; ISession CurrentSession &lt;/span&gt;&lt;br /&gt; &lt;span&gt;{ &lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt; get&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt; &lt;span&gt;      { &lt;/span&gt;&lt;br /&gt; &lt;span&gt;             &lt;span style="color: blue;"&gt;if&lt;/span&gt; (HttpContext.Current==&lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;/span&gt;&lt;br /&gt; &lt;span&gt;             {&lt;/span&gt;&lt;br /&gt; &lt;span&gt;                   &lt;span style="color: blue;"&gt;if&lt;/span&gt; (_session!=&lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;/span&gt;&lt;br /&gt; &lt;span&gt;                   {&lt;/span&gt;&lt;br /&gt; &lt;span&gt;                         &lt;span style="color: blue;"&gt;return&lt;/span&gt; _session;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;                   }&lt;/span&gt;&lt;br /&gt; &lt;span&gt;                   &lt;span style="color: blue;"&gt;else&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;                   {&lt;/span&gt;&lt;br /&gt; &lt;span&gt;                         _session = OpenSession(); &lt;/span&gt;&lt;br /&gt; &lt;span&gt;                         &lt;span style="color: blue;"&gt;return&lt;/span&gt; _session;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;                   }&lt;/span&gt;&lt;br /&gt; &lt;span&gt;             }&lt;/span&gt;&lt;br /&gt; &lt;span&gt;             &lt;span style="color: blue;"&gt;else&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;             {&lt;/span&gt;&lt;br /&gt; &lt;span&gt;             &lt;/span&gt; &lt;span&gt;      &lt;/span&gt; &lt;span&gt; HttpContext currentContext = HttpContext.Current; &lt;/span&gt;&lt;br /&gt; &lt;span&gt;                   ISession session = currentContext.Items[KEY] &lt;span style="color: blue;"&gt;as&lt;/span&gt; ISession; &lt;/span&gt;&lt;br /&gt; &lt;span&gt; &lt;/span&gt;&lt;span&gt;                 &lt;/span&gt; &lt;span style="color: blue;"&gt; if&lt;/span&gt;&lt;span&gt;  (session == &lt;span style="color: blue;"&gt;null&lt;/span&gt;) { &lt;/span&gt;&lt;br /&gt; &lt;span&gt;                   &lt;/span&gt; &lt;span&gt;      &lt;/span&gt; &lt;span&gt;session =  OpenSession(); &lt;/span&gt;&lt;br /&gt; &lt;span&gt;                   &lt;/span&gt; &lt;span&gt;      &lt;/span&gt; &lt;span&gt; currentContext.Items[KEY] = session; &lt;/span&gt;&lt;br /&gt; &lt;span&gt;                   }  &lt;/span&gt;&lt;br /&gt; &lt;span&gt;                   &lt;span style="color: blue;"&gt;return&lt;/span&gt; session; &lt;/span&gt;&lt;br /&gt; &lt;span&gt;             &lt;/span&gt; &lt;span&gt;}&lt;/span&gt;&lt;br /&gt; &lt;span&gt;      } &lt;/span&gt;&lt;br /&gt;&lt;span&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p class="ArticleText"&gt;The code above is rather simple.  We open session once the request started (context_BeginRequest) and put it to the Context.  When session ends we simply get session from Context and close it.  OpenSession is the function responsible for creating NHibernate session instance from session factory object.&lt;/p&gt; &lt;p class="ArticleText"&gt;&lt;span style="color: blue;"&gt; private&lt;/span&gt;&lt;span&gt; &lt;span style="color: blue;"&gt;static&lt;/span&gt; ISessionFactory factory = &lt;span style="color: blue;"&gt;null&lt;/span&gt;;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt; private&lt;/span&gt;&lt;span&gt; &lt;span style="color: blue;"&gt;static&lt;/span&gt; ISessionFactory getFactory()&lt;/span&gt;&lt;br /&gt; &lt;span&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt; if&lt;/span&gt;&lt;span&gt;(factory==&lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;/span&gt;&lt;br /&gt; &lt;span&gt;      {&lt;/span&gt;&lt;br /&gt; &lt;span&gt;             Configuration config;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;             config = &lt;span style="color: blue;"&gt;new&lt;/span&gt; Configuration();&lt;/span&gt;&lt;br /&gt; &lt;span&gt;             &lt;span style="color: blue;"&gt;if&lt;/span&gt; (config==&lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;/span&gt;&lt;br /&gt; &lt;span&gt;             {&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt; throw&lt;/span&gt;&lt;span&gt; &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: teal;"&gt; InvalidOperationException&lt;/span&gt;(&lt;span style="color: maroon;"&gt;"NHibernate  configuration is null."&lt;/span&gt;);&lt;/span&gt;&lt;br /&gt; &lt;span&gt;             }&lt;/span&gt;&lt;br /&gt; &lt;span&gt;             config.AddAssembly(&lt;span style="color: maroon;"&gt;"&lt;/span&gt;&lt;span style="color: blue;"&gt;Dummy.Assembly&lt;/span&gt;&lt;span style="color: maroon;"&gt;"&lt;/span&gt;);&lt;/span&gt;&lt;br /&gt; &lt;span&gt;             factory = config.BuildSessionFactory();&lt;/span&gt;&lt;br /&gt; &lt;span&gt;             &lt;span style="color: blue;"&gt;if&lt;/span&gt; (factory==&lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;/span&gt;&lt;br /&gt; &lt;span&gt;             {&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt; throw&lt;/span&gt;&lt;span&gt; &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: teal;"&gt; InvalidOperationException&lt;/span&gt;(&lt;span style="color: maroon;"&gt;"Call to  Configuration.BuildSessionFactory() returned null."&lt;/span&gt;);&lt;/span&gt;&lt;br /&gt; &lt;span&gt;             }&lt;/span&gt;&lt;br /&gt; &lt;span&gt;      }&lt;/span&gt;&lt;br /&gt; &lt;span&gt;      &lt;span style="color: blue;"&gt;return&lt;/span&gt; factory;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;             &lt;/span&gt;&lt;br /&gt; &lt;span&gt;}&lt;/span&gt;&lt;br /&gt; &lt;span&gt;             &lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt; public&lt;/span&gt;&lt;span&gt; &lt;span style="color: blue;"&gt;static&lt;/span&gt; ISession OpenSession()&lt;/span&gt;&lt;br /&gt; &lt;span&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span&gt;ISession  session;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;session =  getFactory().OpenSession();&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt; if&lt;/span&gt;&lt;span&gt;  (session==&lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;/span&gt;&lt;br /&gt; &lt;span&gt;      {&lt;/span&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt; throw&lt;/span&gt;&lt;span&gt; &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: teal;"&gt; InvalidOperationException&lt;/span&gt;(&lt;span style="color: maroon;"&gt;"Call to  factory.OpenSession() returned null."&lt;/span&gt;);&lt;/span&gt;&lt;br /&gt; &lt;span&gt;      }&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt; return&lt;/span&gt;&lt;span&gt;  session;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p class="ArticleText"&gt;In the code above we start by creating new Configuration object, which holds database connection settings, which in turn we  specified in web.config file (do you remember hibernate.connection.connection_string key from web.config?).  Once configuration is initialized we should tell NHibernate where to find domain objects for our application.  It's good practice to have them in separate assembly along with Data Access layer and Business Logic layer.  After SessionFactory is initialized we can use SessionFactory.OpenSession() method to retrieve new NHibernate session object.&lt;/p&gt; &lt;h2&gt;Building domains&lt;/h2&gt; &lt;p class="ArticleText"&gt; Our ASP.NET application is NHibernate ready, so our next step is to add some domains bound to database tables.  First of all we need to create tables:&lt;/p&gt; &lt;p class="ArticleText"&gt;&lt;span style="color: blue;"&gt;CREATE TABLE&lt;/span&gt; users (&lt;br /&gt;  UserID nvarchar(&lt;span style="color: purple;"&gt;20&lt;/span&gt;) &lt;span style="color: blue;"&gt;NOT NULL default&lt;/span&gt; &lt;span style="color: red;"&gt;'0'&lt;/span&gt;,&lt;br /&gt;  Name nvarchar(&lt;span style="color: purple;"&gt;40&lt;/span&gt;) &lt;span style="color: blue;"&gt;default NULL&lt;/span&gt;,&lt;br /&gt;  &lt;span style="color: blue;"&gt;Password&lt;/span&gt; nvarchar(&lt;span style="color: purple;"&gt;20&lt;/span&gt;) &lt;span style="color: blue;"&gt;default NULL&lt;/span&gt;,&lt;br /&gt;  &lt;span style="color: blue;"&gt;PRIMARY KEY&lt;/span&gt;  (UserID)&lt;br /&gt;)&lt;br /&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;CREATE TABLE&lt;/span&gt;  user_transactions (&lt;br /&gt;  TransID nvarchar(&lt;span style="color: purple;"&gt;20&lt;/span&gt;) &lt;span style="color: blue;"&gt;NOT NULL&lt;/span&gt; default &lt;span style="color: red;"&gt;'0'&lt;/span&gt;,&lt;br /&gt;  UserId nvarchar(&lt;span style="color: purple;"&gt;40&lt;/span&gt;) &lt;span style="color: blue;"&gt;default NULL&lt;/span&gt;,&lt;br /&gt;  TransDate &lt;span style="color: blue;"&gt;datetime&lt;/span&gt; &lt;span style="color: blue;"&gt;default NULL&lt;/span&gt;,&lt;br /&gt;  Amount &lt;span style="color: blue;"&gt;decimal&lt;/span&gt; &lt;span style="color: blue;"&gt;default NULL&lt;/span&gt;,&lt;br /&gt;  &lt;span style="color: blue;"&gt;PRIMARY KEY&lt;/span&gt;  (TransID)&lt;br /&gt;)&lt;/p&gt; &lt;p class="ArticleText"&gt;These are two simple tables where each user can have several roles.  Next step is to create classes for domain objects. This process is pretty straightforward -  we just need to duplicate table structure in the class and provide a way for linking two tables.&lt;/p&gt; &lt;p class="ArticleText"&gt;&lt;span style="color: blue;"&gt; namespace&lt;/span&gt;&lt;span&gt;  Dummy.Assembly&lt;/span&gt;&lt;br /&gt; &lt;span&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span&gt;    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt;  User&lt;/span&gt;&lt;br /&gt; &lt;span&gt;    {&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt;  userId;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt;  name;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt;  password;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        &lt;span style="color: blue;"&gt;private&lt;/span&gt; IList transactions;&lt;/span&gt;&lt;br /&gt; &lt;span&gt; &lt;/span&gt;&lt;br /&gt; &lt;span&gt;        &lt;span style="color: blue;"&gt;public&lt;/span&gt; User()&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        {&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        }&lt;/span&gt;&lt;br /&gt; &lt;span&gt; &lt;/span&gt;&lt;br /&gt; &lt;span&gt;        &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt; Id&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        {&lt;/span&gt;&lt;br /&gt; &lt;span&gt;             &lt;span style="color: blue;"&gt;get&lt;/span&gt; { &lt;span style="color: blue;"&gt; return&lt;/span&gt; userId; }&lt;/span&gt;&lt;br /&gt; &lt;span&gt;             &lt;span style="color: blue;"&gt;set&lt;/span&gt; { userId = &lt;span style="color: blue;"&gt;value&lt;/span&gt;; }&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        }&lt;/span&gt;&lt;br /&gt; &lt;span&gt; &lt;/span&gt;&lt;br /&gt; &lt;span&gt;        &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt;  Name&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        {&lt;/span&gt;&lt;br /&gt; &lt;span&gt;             &lt;span style="color: blue;"&gt;get&lt;/span&gt; { &lt;span style="color: blue;"&gt;return&lt;/span&gt;  name; }&lt;/span&gt;&lt;br /&gt; &lt;span&gt;             &lt;span style="color: blue;"&gt;set&lt;/span&gt; { name = &lt;span style="color: blue;"&gt;value&lt;/span&gt;; }&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        }&lt;/span&gt;&lt;br /&gt; &lt;span&gt; &lt;/span&gt;&lt;br /&gt; &lt;span&gt;        &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt;  Password&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        {&lt;/span&gt;&lt;br /&gt; &lt;span&gt;             &lt;span style="color: blue;"&gt;get&lt;/span&gt; { &lt;span style="color: blue;"&gt; return&lt;/span&gt; password; }&lt;/span&gt;&lt;br /&gt; &lt;span&gt;             &lt;span style="color: blue;"&gt;set&lt;/span&gt; { password = &lt;span style="color: blue;"&gt;value&lt;/span&gt;; }&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        }&lt;/span&gt;&lt;br /&gt; &lt;span&gt; &lt;/span&gt;&lt;br /&gt; &lt;span&gt;        &lt;span style="color: blue;"&gt;public&lt;/span&gt; IList Transactions&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        {&lt;/span&gt;&lt;br /&gt; &lt;span&gt;             &lt;span style="color: blue;"&gt;get&lt;/span&gt; { &lt;span style="color: blue;"&gt;return&lt;/span&gt;  transactions; }&lt;/span&gt;&lt;br /&gt; &lt;span&gt;             &lt;/span&gt; &lt;span style="color: blue;"&gt; set&lt;/span&gt;&lt;span&gt; {  transactions = &lt;span style="color: blue;"&gt;value&lt;/span&gt;; }&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        }&lt;/span&gt;&lt;br /&gt; &lt;span&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;span&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p class="ArticleText"&gt;As transactions and users have many-to-one relationships we've created a property to hold a list of transactions belonging to this user. Transaction class is similar to User class but instead of IList it holds an instance of User object.&lt;/p&gt; &lt;p class="ArticleText"&gt;&lt;span style="color: blue;"&gt; namespace&lt;/span&gt;&lt;span&gt;  Dummy.Assembly&lt;/span&gt;&lt;br /&gt; &lt;span&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span&gt;    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt;  Transaction&lt;/span&gt;&lt;br /&gt; &lt;span&gt;    {&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt;  transId;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        &lt;span style="color: blue;"&gt;private&lt;/span&gt; User user;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        &lt;span style="color: blue;"&gt;private&lt;/span&gt; DateTime transDate;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;decimal&lt;/span&gt;  amount;&lt;/span&gt;&lt;br /&gt; &lt;span&gt; &lt;/span&gt;&lt;br /&gt; &lt;span&gt;        &lt;span style="color: blue;"&gt;public&lt;/span&gt; Transaction()&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        {&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        }&lt;/span&gt;&lt;br /&gt; &lt;span&gt; &lt;/span&gt;&lt;br /&gt; &lt;span&gt;        &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt; Id&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        {&lt;/span&gt;&lt;br /&gt; &lt;span&gt;             &lt;span style="color: blue;"&gt;get&lt;/span&gt; { &lt;span style="color: blue;"&gt; return&lt;/span&gt; transId; }&lt;/span&gt;&lt;br /&gt; &lt;span&gt;             &lt;span style="color: blue;"&gt;set&lt;/span&gt; { transId = &lt;span style="color: blue;"&gt;value&lt;/span&gt;; }&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        }&lt;/span&gt;&lt;br /&gt; &lt;span&gt; &lt;/span&gt;&lt;br /&gt; &lt;span&gt;        &lt;span style="color: blue;"&gt;public&lt;/span&gt; User User&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        {&lt;/span&gt;&lt;br /&gt; &lt;span&gt;             &lt;span style="color: blue;"&gt;get&lt;/span&gt; { &lt;span style="color: blue;"&gt; return&lt;/span&gt; user; }&lt;/span&gt;&lt;br /&gt; &lt;span&gt;             &lt;span style="color: blue;"&gt;set&lt;/span&gt; { user = &lt;span style="color: blue;"&gt;value&lt;/span&gt;;  }&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        }&lt;/span&gt;&lt;br /&gt; &lt;span&gt; &lt;/span&gt;&lt;br /&gt; &lt;span&gt;        &lt;span style="color: blue;"&gt;public&lt;/span&gt; DateTime TransDate&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        {&lt;/span&gt;&lt;br /&gt; &lt;span&gt;             &lt;span style="color: blue;"&gt;get&lt;/span&gt; { &lt;span style="color: blue;"&gt; return&lt;/span&gt; transDate; }&lt;/span&gt;&lt;br /&gt; &lt;span&gt;             &lt;span style="color: blue;"&gt;set&lt;/span&gt; { transDate = &lt;span style="color: blue;"&gt;value&lt;/span&gt;; }&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        }&lt;/span&gt;&lt;br /&gt; &lt;span&gt; &lt;/span&gt;&lt;br /&gt; &lt;span&gt;        &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;decimal&lt;/span&gt;  Amount&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        {&lt;/span&gt;&lt;br /&gt; &lt;span&gt;             &lt;span style="color: blue;"&gt;get&lt;/span&gt; { &lt;span style="color: blue;"&gt; return&lt;/span&gt; amount; }&lt;/span&gt;&lt;br /&gt; &lt;span&gt;             &lt;/span&gt; &lt;span style="color: blue;"&gt; set&lt;/span&gt;&lt;span&gt; {  amount = &lt;span style="color: blue;"&gt;value&lt;/span&gt;; }&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        }&lt;/span&gt;&lt;br /&gt; &lt;span&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;span&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p class="ArticleText"&gt;Once domain classes are ready we should bind classes to tables and tell  NHibernate which column matches which property from the domain class.  This is done view hibernate mapping files  - which must be placed in the namespace we've specified as a parameter for SessionFactory class constructor. &lt;/p&gt; &lt;p class="ArticleText"&gt;Important note: User.hbm.xml and Transaction.hbm.xml files should have "Embeded resource" type.&lt;/p&gt; &lt;p class="ArticleText"&gt;Mapping files shown above should have name in following format: &amp;lt;ClassName&amp;gt;.hbm.xml (.hbm - means Hibernate mapping). They are pretty simple: class tag tells hibernate to map Dummy.Assembly. User class form assembly named Dummy.Assembly to table users; id denotes primary key field (there must be primary key field in every domain). Property tags does all the magic with mapping properties (name attribute) of the class to respective column of table (column attribute). And more complex bag and many-to-one tags are used to specify relationship between two domains. Bag is used to map one-to-many relations by means of specifying foreign key (&amp;lt;key column="UserId" /&amp;gt;) in table user_transactions and specifying target relation (one-to-many tag which tells NHibernate that foreign key is found in Transaction class). NHibernate while loading user from table will also take user primary key value and load data from Transaction table, making sure that property UserId is the same as user primary key value. Once all transactions are loaded they will be placed in respective collection - in our case it's Transaction in User class. Another important part there is lazy attribute of bag tag. Let's imagine we have 1 million users and each of them have 1 million transactions. When we want to load all users and reset their passwords at once we will force a big performance problem. That's because NHibernate will load us 1,000,000*1,000,000 records from the database, but we don't need transactions at the moment. Lazy loading is what will help us. Lazy loaded collection is not loaded once its parent object initialized, it only loads when our classes access this collection. Simply setting most of the relations to be lazy will boost performance a lot.&lt;/p&gt; &lt;p class="ArticleText"&gt;Once our mapping files are ready and placed in proper assembly along with domain classes we can go further.  Now we need to use NHibernate session to manage our domain objects. The following code creates new user:&lt;/p&gt; &lt;p class="ArticleText"&gt;&lt;span style="color: teal;"&gt; ITransaction&lt;/span&gt;&lt;span&gt;  transaction = session.BeginTransaction();&lt;/span&gt;&lt;br /&gt; &lt;span style="color: teal;"&gt; User&lt;/span&gt;&lt;span&gt;  u = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: teal;"&gt;User&lt;/span&gt;();&lt;/span&gt;&lt;br /&gt; &lt;span&gt;u.Id =  "admin";&lt;/span&gt;&lt;br /&gt; &lt;span&gt;u.Name =  "Administrator";&lt;/span&gt;&lt;br /&gt; &lt;span&gt; u.Password = "secret";&lt;/span&gt;&lt;br /&gt; &lt;span&gt; &lt;/span&gt;&lt;br /&gt; &lt;span&gt; session.Save(u);&lt;/span&gt;&lt;br /&gt; &lt;span&gt; transaction.Commit();&lt;/span&gt;&lt;br /&gt; &lt;span&gt; &lt;/span&gt;&lt;br /&gt; &lt;span&gt; transaction = session.BeginTransaction();&lt;/span&gt;&lt;br /&gt; &lt;span&gt;u.Name =  "Admin";&lt;/span&gt;&lt;br /&gt; &lt;span&gt; session.Update(u);&lt;/span&gt;&lt;br /&gt;&lt;span&gt;transaction.Commit();&lt;/span&gt;&lt;/p&gt; &lt;p class="ArticleText"&gt;Session variable is the NHibernate session we've instantiated in our HTTP module class.  Example is fair simple. We simply create new domain object,  fill its fields and tell the session to save changes. After that domain object will be persisted to database.  In the next step we change user name and update the record in database. Now we need to load this user into our domain object:&lt;/p&gt; &lt;p class="ArticleText"&gt;User admin = (User) session.Load(&lt;span style="color: blue;"&gt;typeof&lt;/span&gt;(User), &lt;span style="color: maroon;"&gt;"admin"&lt;/span&gt;);&lt;/p&gt; &lt;p class="ArticleText"&gt;Load method loads user record identified by primary key value  "admin" from database into our domain object. Isn't it simple?&lt;/p&gt; &lt;h2&gt;Queries and Criteria&lt;/h2&gt; &lt;p class="ArticleText"&gt;Loading objects by id good, but what to do when we need to load all objects or filter them by some criteria? Here comes HQL - a special query language used to retrieve data in NHibernate. It's as simple as SQL and moreover - it's SQL adapted for NHibernate. HQL instead of table columns uses properties from domain objects, instead of tables - domains. HQL is case sensitive, which means property names should be written in HQL just like in domain object. Those are the main differences. Examples of HQL below:&lt;/p&gt; &lt;p class="ArticleText"&gt;&lt;span style="color: blue;"&gt;FROM&lt;/span&gt;&lt;span&gt; &lt;span style="color: fuchsia;"&gt;User &lt;/span&gt;&lt;span style="color: blue;"&gt;WHERE&lt;/span&gt;  Name=&lt;span style="color: red;"&gt;'Admin'&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;SELECT &lt;/span&gt; &lt;span style="color: fuchsia;"&gt;COUNT&lt;/span&gt;&lt;span style="color: rgb(153, 153, 153);"&gt;(*)&lt;/span&gt;&lt;span&gt; &lt;span style="color: blue;"&gt;FROM&lt;/span&gt; &lt;span style="color: fuchsia;"&gt;User&lt;/span&gt; u&lt;/span&gt;&lt;/p&gt; &lt;p class="ArticleText"&gt;In HQL you're allowed to drop SELECT statement.  In this case all Users will be selected. To run query you should use simple query class provided with NHibernate:&lt;/p&gt; &lt;p class="ArticleText"&gt;&lt;span&gt;IQuery query =  session.CreateQuery(&lt;span style="color: maroon;"&gt;"FROM User WHERE Name=:name"&lt;/span&gt;);&lt;/span&gt;&lt;br /&gt; &lt;span&gt;query.SetString(&lt;span style="color: maroon;"&gt;"name"&lt;/span&gt;, &lt;span style="color: maroon;"&gt;"Admin"&lt;/span&gt;);&lt;/span&gt;&lt;br /&gt;&lt;span&gt; IList users = query.List();&lt;/span&gt;&lt;/p&gt; &lt;p class="ArticleText"&gt;We simply create a query, then pass required parameters and execute  List method which will return a collection of Users (even if there only one user).  To retrieve only one object from query (as in example with count) you can use UniqueResult method of IQuery.&lt;/p&gt; &lt;p class="ArticleText"&gt;Another way to load your objects is to use ICriteria interface.  Using criteria can be considered more OOP-oriented rather than using queries.  Both have their advantages and drawbacks. Using criteria is as simple as using query:&lt;/p&gt; &lt;p class="ArticleText"&gt;&lt;span&gt;ICriteria criteria =  session.CreateCriteria(&lt;span style="color: blue;"&gt;typeof&lt;/span&gt;(User));&lt;/span&gt;&lt;br /&gt; &lt;span&gt; criteria.Add(Expression.Eq(&lt;span style="color: maroon;"&gt;"Name"&lt;/span&gt;, &lt;span style="color: maroon;"&gt;"Admin"&lt;/span&gt;));&lt;br /&gt;IList users = criteria.List();&lt;/span&gt;&lt;/p&gt; &lt;p class="ArticleText"&gt;We create criteria for user class, and then, using  Expression class we tell the criteria to load users that have their "Name" field equal (Eq) "Admin".&lt;/p&gt; &lt;p class="ArticleText"&gt;It's seems easy to use NHibernate - but really it's not that easy.  The main problem is that you can't test your query unless you run your code  (SQL query you can run in special SQL editors/analyzers and NHibernate does not have one).  Another problem is multiple updates of single instance of domain object is common for the beginners. &lt;/p&gt; &lt;h2&gt;Adding Log4Net to ASP.NET application&lt;/h2&gt; &lt;p class="ArticleText"&gt;To track possible problems Log4Net can be used. Its setup is easy.  To make it work with ASP.NET you should add following lines to web.config file:&lt;/p&gt; &lt;p class="ArticleText"&gt;&lt;span style="color: blue;"&gt;  &amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;configSections&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;    . . . &lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;section&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt; name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;log4net&lt;/span&gt;"&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;    &lt;/span&gt;&lt;span style="color: red;"&gt; type&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;log4net.Config.Log4NetConfigurationSectionHandler,log4net&lt;/span&gt;"&lt;span style="color: blue;"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;  &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;configSections&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;  &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;log4net&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;appender&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt; name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;GeneralLog&lt;/span&gt;"&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;type&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;log4net.Appender.RollingFileAppender&lt;/span&gt;"&lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;      &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;file&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt; value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;Logs/general.txt&lt;/span&gt;"&lt;span style="color: blue;"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;      &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;appendToFile&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt; value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;true&lt;/span&gt;"&lt;span style="color: blue;"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;      &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;maximumFileSize&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt; value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;100KB&lt;/span&gt;"&lt;span style="color: blue;"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;      &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;rollingStyle&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt; value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;Size&lt;/span&gt;"&lt;span style="color: blue;"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;      &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;layout&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt; type&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;log4net.Layout.PatternLayout&lt;/span&gt;"&lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;        &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;conversionPattern&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt; value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;%d{HH:mm:ss}  [%t] %-5p %c - %m%n&lt;/span&gt;"&lt;span style="color: blue;"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;      &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;layout&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;appender&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;root&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;      &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;level&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt; value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;DEBUG&lt;/span&gt;"&lt;span style="color: blue;"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;      &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;appender-ref&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;ref&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;GeneralLog&lt;/span&gt;"&lt;span style="color: blue;"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;root&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;logger&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt; name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;NHibernate&lt;/span&gt;"&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;additivity&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;false&lt;/span&gt;"&lt;span style="color: blue;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;      &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;level&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt; value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;DEBUG&lt;/span&gt;"&lt;span style="color: blue;"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;      &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;appender-ref&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;ref&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;  GeneralLog&lt;/span&gt;"&lt;span style="color: blue;"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;    &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;logger&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;  &lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;log4net&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;   . . . &lt;/span&gt;&lt;/p&gt; &lt;p class="ArticleText"&gt;Log4Net is based on appenders and loggers. Appenders are used to write logs to some destination (in our example - flat file), while loggers determine which appender should certain namespace use. There is always one appender and root logger (it logs from all classes). Additional loggers have a mandatory attribute name which specifies the namespace to which this logger is applied. For example - you can have a separate log files for Data Access layer and Business Layer. Logger level determines which of the log messages should be printed to log. There are several levels - DEBUG, INFO, WARN, ERROR, FATAL. Debug - include all levels, while Warn include Warn, Error and Fatal and so on.&lt;/p&gt; &lt;p class="ArticleText"&gt;The next thing is to add to our NHibernate HTTP module's context_BeginRequest procedure following lines to initialize log4net logging system:&lt;/p&gt; &lt;p class="ArticleText"&gt;&lt;span&gt; log4net.Config.&lt;span style="color: teal;"&gt;XmlConfigurator&lt;/span&gt;.Configure();&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span&gt;or&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;log4net.Config.&lt;span style="color: teal;"&gt;BasicConfigurator&lt;/span&gt;.Configure();&lt;/p&gt; &lt;p class="ArticleText"&gt;Now we can use Log4Net:&lt;/p&gt; &lt;p class="ArticleText"&gt;&lt;span style="color: blue;"&gt; private&lt;/span&gt;&lt;span&gt; &lt;span style="color: blue;"&gt;static&lt;/span&gt; ILog log = LogManager.GetLogger(&lt;span style="color: blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color: teal;"&gt;User&lt;/span&gt;));&lt;/span&gt;&lt;br /&gt; &lt;span&gt; &lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt; public&lt;/span&gt;&lt;span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; SomeMethod() {&lt;/span&gt;&lt;br /&gt; &lt;span&gt;       log.Info(&lt;span style="color: maroon;"&gt;"SomeMethod called"&lt;/span&gt;);&lt;/span&gt;&lt;br /&gt; &lt;span&gt;      &lt;span style="color: blue;"&gt;try&lt;/span&gt; {&lt;/span&gt;&lt;br /&gt; &lt;span&gt;             session.Load(&lt;span style="color: blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color: teal;"&gt;User&lt;/span&gt;), &lt;span style="color: maroon;"&gt;"admin"&lt;/span&gt;);&lt;/span&gt;&lt;br /&gt;&lt;span&gt;} &lt;span style="color: blue;"&gt;catch&lt;/span&gt; (&lt;span style="color: teal;"&gt;Exception&lt;/span&gt;  ex) {&lt;/span&gt;&lt;br /&gt; &lt;span&gt;             log.Error(&lt;span style="color: maroon;"&gt;"Failed to load amin user"&lt;/span&gt;);&lt;/span&gt;&lt;br /&gt; &lt;span&gt;             log.Debug(&lt;span style="color: maroon;"&gt;"Stack trace follows"&lt;/span&gt;, ex);&lt;/span&gt;&lt;br /&gt; &lt;span&gt;} &lt;span style="color: blue;"&gt;finally&lt;/span&gt; {&lt;/span&gt;&lt;br /&gt; &lt;span&gt;             log.Info(&lt;span style="color: maroon;"&gt;"SomeMethod end"&lt;/span&gt;);&lt;/span&gt;&lt;br /&gt; &lt;span&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p class="ArticleText"&gt;In example we create logger, giving it the typeof(User). This will be used by Log4Net to determine what logger and appender from web.config file to use. Next we generate some log data of different levels (Info, Error, Debug). As our root logger is set to Debug level we will see all the messages. When we don't want a mess in our logs, we can simply change level of appropriate logger to Error. Then no messages from log.Debug and log.Info methods will appear in our log. That's good while using NHibernate because it likes logging much and this decrease performance and log files will grow fast. So it's a good idea to lower log level while setting up live server. Also it's a good practice to have log output in all try-catch blocks.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2770957963577646170-5610086309870841346?l=ravi-testingblog.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ravi-testingblog.blogspot.com/feeds/5610086309870841346/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ravi-testingblog.blogspot.com/2009/07/using-nhibernate-and-log4net-in-aspnet.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2770957963577646170/posts/default/5610086309870841346'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2770957963577646170/posts/default/5610086309870841346'/><link rel='alternate' type='text/html' href='http://ravi-testingblog.blogspot.com/2009/07/using-nhibernate-and-log4net-in-aspnet.html' title='Using NHibernate and Log4Net in ASP.NET 2.0 applications'/><author><name>ravi</name><uri>http://www.blogger.com/profile/07517056757956161890</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2770957963577646170.post-4732616956274890944</id><published>2009-07-28T10:45:00.000-07:00</published><updated>2009-07-28T10:51:19.809-07:00</updated><title type='text'>Make Charts in ASP.NET 2.0</title><content type='html'>&lt;h2&gt;Introduction&lt;/h2&gt; &lt;p class="ArticleText"&gt;Graphs and charts are important tools for the analysis of data. In this tutorial, we will look at how to represent data in terms of a bar chart and pie chart. To make this possible, the latest rendition of Microsoft's Graphics Device Interface (GDI+) was brought on the market.&lt;br /&gt;&lt;/p&gt;&lt;h2&gt;Scenarios to draw bar and pie charts &lt;/h2&gt; &lt;p class="ArticleText"&gt;In our &lt;a href="http://www.beansoftware.com/ASP.NET-Tutorials/Examples/Bar-Pie-Charts.zip"&gt;bar and pie chart example&lt;/a&gt;, we will represent the data of a fictitious country having 4 towns;  namely: Town A, Town B, Town C and Town D.  Their population will be modeled in terms of graphs and charts.   The figures are given below:&lt;/p&gt; &lt;div align="center"&gt;&lt;p class="ArticleText"&gt;&lt;table border="0" width="178"&gt;   &lt;tbody&gt;&lt;tr bgcolor="#cccccc"&gt;     &lt;td width="57"&gt;&lt;div align="center"&gt;Town&lt;/div&gt;&lt;/td&gt;     &lt;td width="111"&gt;&lt;div align="center"&gt;Population&lt;/div&gt;&lt;/td&gt;   &lt;/tr&gt;   &lt;tr&gt;     &lt;td&gt;A&lt;/td&gt;     &lt;td&gt;1,000,000&lt;/td&gt;   &lt;/tr&gt;   &lt;tr&gt;     &lt;td&gt;B&lt;/td&gt;     &lt;td&gt;600,000&lt;/td&gt;   &lt;/tr&gt;   &lt;tr&gt;     &lt;td&gt;C&lt;/td&gt;     &lt;td&gt;2,500,000&lt;/td&gt;   &lt;/tr&gt;   &lt;tr&gt;     &lt;td&gt;D&lt;/td&gt;     &lt;td&gt;800,000&lt;/td&gt;   &lt;/tr&gt; &lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;/div&gt; &lt;p&gt; &lt;/p&gt; &lt;div align="center"&gt;&lt;img src="http://www.beansoftware.com/ASP.NET-Tutorials/Images/Chart-Sample.jpg" border="0" /&gt;&lt;br /&gt;&lt;p class="ArticleText"&gt;Bar chart that should be outputted&lt;/p&gt;&lt;/div&gt; &lt;div align="center"&gt;&lt;img src="http://www.beansoftware.com/ASP.NET-Tutorials/Images/Chart-Pie.jpg" border="0" /&gt;&lt;br /&gt;&lt;p class="ArticleText"&gt;Pie chart that should be outputted&lt;/p&gt;&lt;/div&gt;  &lt;h2&gt;Useful classes&lt;/h2&gt;  &lt;p class="ArticleText"&gt;Below are some of the useful classes that will be used to achieve what we want:&lt;br /&gt; &lt;br /&gt;  &lt;b&gt;The Bitmap class&lt;/b&gt;&lt;br /&gt; &lt;br /&gt;In windows application, you would have output graphical contents to one of the windows. Whereas in ASP.NET, there are no window. Fortunately, there is a class that can act as the drawing surface and that is the Bitmap class.&lt;/p&gt; &lt;p class="ArticleText"&gt;As any drawing surface has a width and a height, so does the Bitmap object. So, when we will create a Bitmap object, we will pass in a width and a height as parameters in the constructors. Moreover, we have to specify “Pixel Format Argument” which means whether the pixel format should be 24-bits RGB color, 32-bits RGB color, GDI-colors etc.. In short the following code will do the work.&lt;/p&gt;  &lt;p class="ArticleText"&gt; &lt;span style="color: blue;"&gt; Dim&lt;/span&gt;&lt;span&gt;  Image &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt;  Bitmap(500, 300, PixelFormat.Format32bppRgb)&lt;/span&gt;&lt;/p&gt;  &lt;h2&gt;The Graphics class&lt;/h2&gt; &lt;p class="ArticleText"&gt;&lt;br /&gt;&lt;iframe marginheight="0" marginwidth="0" src="http://www.beansoftware.com/ads/LQ-Rectangle.aspx" scrolling="no" frameborder="0" height="280" width="336"&gt;&lt;/iframe&gt;&lt;br /&gt;Once you have created the Bitmap object,  you need to obtain a Graphics object that references that Bitmap object.   The following code will do the job.&lt;/p&gt; &lt;p class="ArticleText"&gt;&lt;span style="color: green;"&gt; ' Get the graphics context for the bitmap.&lt;/span&gt; &lt;span style="color: blue;"&gt;&lt;br /&gt;Dim&lt;/span&gt;&lt;span&gt; g &lt;span style="color: blue;"&gt;As&lt;/span&gt; Graphics = Graphics.FromImage(Image)&lt;/span&gt;&lt;/p&gt;  &lt;p class="ArticleText"&gt;The x vs. y coordinates system is illustrated in the figure below.&lt;/p&gt;  &lt;div align="center"&gt;&lt;img src="http://www.beansoftware.com/ASP.NET-Tutorials/Images/Chart-Coordinate.jpg" border="0" /&gt;&lt;br /&gt;&lt;p class="ArticleText"&gt;Coordinate axes for the Bitmap object&lt;/p&gt;&lt;/div&gt;  &lt;h2&gt;The Fun begins&lt;/h2&gt; &lt;p class="ArticleText"&gt;Now that you have a â€œsurface to drawâ€, as you may have guessed,  you will need a pen or brush to draw.  To create a pen, a few properties of the pen should be specified;  like the color of the pen and the width of the pen...&lt;/p&gt;  &lt;p class="ArticleText"&gt;&lt;span style="color: green;"&gt; ' Create a pen for drawing&lt;/span&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt; Dim&lt;/span&gt;&lt;span&gt;  redPen &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt;  Pen(Color.Red, 10)&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="color: green;"&gt; ' Create a blue brush&lt;/span&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt; Dim&lt;/span&gt;&lt;span&gt;  blueBrush &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt;      SolidBrush(Color.Blue)&lt;/span&gt;&lt;/p&gt;  &lt;h2&gt;Useful methods for drawing&lt;/h2&gt; &lt;p class="ArticleText"&gt;Now that we have a set of drawing tools at our disposal, the graphics class also has a set of interesting methods to simplify our work for drawing objects like: circles, rectangles, text and the list goes on.&lt;br /&gt;&lt;br /&gt;For this tutorial, we will be using the following self explanatory methods. &lt;/p&gt;   &lt;div align="center"&gt;&lt;table style="" cellpadding="0" cellspacing="0" width="498"&gt;   &lt;tbody&gt;&lt;tr bgcolor="#cccccc"&gt;     &lt;td valign="top" width="215"&gt;&lt;p align="center"&gt;&lt;strong&gt;Method Name &lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;     &lt;td valign="top" width="375"&gt;&lt;p align="center"&gt;&lt;strong&gt;Purpose &lt;/strong&gt;&lt;/p&gt;&lt;/td&gt;   &lt;/tr&gt;   &lt;tr&gt;     &lt;td valign="top" width="215"&gt;&lt;p&gt;Clear ( ) &lt;/p&gt;&lt;/td&gt;     &lt;td valign="top" width="375"&gt;&lt;p&gt;Cleans the graphics object &lt;/p&gt;         &lt;p&gt;(Pretty much like cleaning the whiteboard) &lt;/p&gt;&lt;/td&gt;   &lt;/tr&gt;   &lt;tr&gt;     &lt;td valign="top" width="215"&gt;&lt;p&gt;DrawString ( ) &lt;/p&gt;&lt;/td&gt;     &lt;td valign="top" width="375"&gt;&lt;p&gt;For outputting text &lt;/p&gt;&lt;/td&gt;   &lt;/tr&gt;   &lt;tr&gt;     &lt;td valign="top" width="215"&gt;&lt;p&gt;FillPie ( ) &lt;/p&gt;&lt;/td&gt;     &lt;td valign="top" width="375"&gt;&lt;p&gt;Draw a pie slice (useful for pie chart) &lt;/p&gt;&lt;/td&gt;   &lt;/tr&gt;   &lt;tr&gt;     &lt;td valign="top" width="215"&gt;&lt;p&gt;FillRectangle ( ) &lt;/p&gt;&lt;/td&gt;     &lt;td valign="top" width="375"&gt;&lt;p&gt;Draws a rectangle (useful for bar charts) &lt;/p&gt;&lt;/td&gt;   &lt;/tr&gt;   &lt;tr&gt;     &lt;td valign="top" width="215"&gt;&lt;p&gt;DrawLine ( ) &lt;/p&gt;&lt;/td&gt;     &lt;td valign="top" width="375"&gt;&lt;p&gt;Draws a line &lt;/p&gt;&lt;/td&gt;   &lt;/tr&gt; &lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt; &lt;p class="ArticleText"&gt; N.B. the above methods may be overloaded, so we used the names only.   It is left to you as an option to explore how the overloaded methods differ.&lt;/p&gt;  &lt;p class="ArticleText"&gt;&lt;strong&gt;Some concerns needed to draw bar charts and pie charts &lt;/strong&gt;&lt;/p&gt; &lt;ul class="ArticleText"&gt;&lt;li&gt;As the y-axis is reversed, some further calculations need to be done to place the bar charts on the bitmap object. &lt;/li&gt;&lt;li&gt;We shall use one color representing each town. &lt;/li&gt;&lt;li&gt;The formula for calculating a sector in a pie chart for a population[i] is:&lt;br /&gt; (population[i] / totalPopulation) * 360&lt;/li&gt;&lt;/ul&gt;  &lt;h2&gt;Coding the Chart Generator functions&lt;/h2&gt; &lt;p class="ArticleText"&gt;First of all create 2 buttons and label them â€œBarchartâ€ and â€œPiechartâ€ as shown in the figure below:&lt;/p&gt; &lt;div align="center"&gt;&lt;img src="http://www.beansoftware.com/ASP.NET-Tutorials/Images/Chart-Application.jpg" border="0" /&gt;&lt;br /&gt;&lt;/div&gt; &lt;p class="ArticleText"&gt;Next, we shall declare some variables for the Bitmap object, Graphics object,  population values, town names and the color representing each town.   The code is as follows:&lt;/p&gt; &lt;p class="ArticleText"&gt; &lt;span style="color: green;"&gt; '   Variables declaration&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt; Private&lt;/span&gt;&lt;span&gt;  myImage &lt;span style="color: blue;"&gt;As&lt;/span&gt; Bitmap&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt; Private&lt;/span&gt;&lt;span&gt;  g &lt;span style="color: blue;"&gt;As&lt;/span&gt; Graphics&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt; Private&lt;/span&gt;&lt;span&gt;  p() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt; =  {1000000, 600000, 2500000, 80000}&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt; Private&lt;/span&gt;&lt;span&gt;  towns() &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;String&lt;/span&gt;  = {&lt;span style="color: maroon;"&gt;"A"&lt;/span&gt;, &lt;span style="color: maroon;"&gt;"B"&lt;/span&gt;, &lt;span style="color: maroon;"&gt;"C"&lt;/span&gt;, &lt;span style="color: maroon;"&gt;"D"&lt;/span&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt; Private&lt;/span&gt;&lt;span&gt;  myBrushes(4) &lt;span style="color: blue;"&gt;As&lt;/span&gt; Brush&lt;/span&gt;&lt;br /&gt;&lt;/p&gt;  &lt;p class="ArticleText"&gt;Next, in our Page_Load event, we will call a function that will create  objects from the Bitmap and Graphics classes.   We should also create the brushes that will be used for drawing the charts.  The code snippet below does the job:&lt;/p&gt; &lt;p class="ArticleText"&gt; &lt;span style="color: green;"&gt; ' Create an in-memory bitmap where you will draw the image. &lt;/span&gt;&lt;br /&gt; &lt;span style="color: green;"&gt; ' The Bitmap is 300 pixels wide and 200 pixels high.&lt;/span&gt;&lt;br /&gt; &lt;span&gt;myImage = &lt;span style="color: blue;"&gt;New&lt;/span&gt; Bitmap(500, 300, PixelFormat.Format32bppRgb)&lt;/span&gt;&lt;br /&gt; &lt;span&gt; &lt;/span&gt;&lt;br /&gt; &lt;span style="color: green;"&gt; ' Get the graphics context for the bitmap.&lt;/span&gt;&lt;br /&gt; &lt;span&gt;g =  Graphics.FromImage(myImage)&lt;/span&gt;&lt;br /&gt; &lt;span&gt; &lt;/span&gt;&lt;br /&gt; &lt;span style="color: green;"&gt; '   Create the brushes for drawing&lt;/span&gt;&lt;br /&gt; &lt;span&gt; myBrushes(0) = &lt;span style="color: blue;"&gt;New&lt;/span&gt; SolidBrush(Color.Red)&lt;/span&gt;&lt;br /&gt; &lt;span&gt; myBrushes(1) = &lt;span style="color: blue;"&gt;New&lt;/span&gt; SolidBrush(Color.Blue)&lt;/span&gt;&lt;br /&gt; &lt;span&gt; myBrushes(2) = &lt;span style="color: blue;"&gt;New&lt;/span&gt; SolidBrush(Color.Yellow)&lt;/span&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span&gt; myBrushes(3) = &lt;span style="color: blue;"&gt;New&lt;/span&gt; SolidBrush(Color.Green)&lt;/span&gt;&lt;br /&gt;&lt;/p&gt;  &lt;p class="ArticleText"&gt; The bar charts have to be placed in a manner that we could draw the axes and label them as well.   So, we declare an interval variable that will space the bar charts.&lt;/p&gt;  &lt;p class="ArticleText"&gt; &lt;span style="color: green;"&gt; '   Variables declaration&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt; Dim&lt;/span&gt;&lt;span&gt;  i &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt; Dim&lt;/span&gt;&lt;span&gt;  xInterval &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;  = 100&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt; Dim&lt;/span&gt;&lt;span&gt;  width &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;  = 90&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt; Dim&lt;/span&gt;&lt;span&gt;  height &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt; Dim&lt;/span&gt;&lt;span&gt;  blackBrush &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt;  SolidBrush(Color.Black)&lt;/span&gt;&lt;br /&gt; &lt;span&gt; &lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt; For&lt;/span&gt;&lt;span&gt;  i = 0 &lt;span style="color: blue;"&gt;To&lt;/span&gt; p.Length - 1&lt;/span&gt;&lt;br /&gt; &lt;span&gt;    height  = (p(i) \ 10000) &lt;span style="color: green;"&gt;'   divide by 10000 to adjust  barchart to height of Bitmap&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: green;"&gt;  &lt;/span&gt;&lt;br /&gt; &lt;span&gt;    &lt;span style="color: green;"&gt;'   Draws the bar chart using specific colours&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;     g.FillRectangle(myBrushes(i), xInterval * i + 50, 280 - height, width, height)&lt;/span&gt;&lt;br /&gt; &lt;span&gt; &lt;/span&gt;&lt;br /&gt; &lt;span&gt;    &lt;span style="color: green;"&gt;'   label the barcharts&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;     g.DrawString(towns(i), &lt;span style="color: blue;"&gt;New&lt;/span&gt; Font(&lt;span style="color: maroon;"&gt;"Verdana"&lt;/span&gt;,  12, FontStyle.Bold), Brushes.Black, xInterval * i + 50 + (width / 3), 280 -  height - 25)&lt;/span&gt;&lt;br /&gt; &lt;span&gt; &lt;/span&gt;&lt;br /&gt; &lt;span&gt;    &lt;span style="color: green;"&gt;'   Draw the scale&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;     g.DrawString(height, &lt;span style="color: blue;"&gt;New&lt;/span&gt; Font(&lt;span style="color: maroon;"&gt;"Verdana"&lt;/span&gt;,  8, FontStyle.Bold), Brushes.Black, 0, 280 - height)&lt;/span&gt;&lt;br /&gt; &lt;span&gt; &lt;/span&gt;&lt;br /&gt; &lt;span&gt;    &lt;span style="color: green;"&gt;'   Draw the axes&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;     g.DrawLine(Pens.Brown, 40, 10, 40, 290)         &lt;span style="color: green;"&gt;'    y-axis&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;     g.DrawLine(Pens.Brown, 20, 280, 490, 280)       &lt;span style="color: green;"&gt;'    x-axis&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt; Next&lt;/span&gt;&lt;/p&gt;  &lt;p class="ArticleText"&gt; The above code has only to be linked with the Click event of the â€œBarchartâ€ button that was placed ok the page before. &lt;/p&gt;&lt;p class="ArticleText"&gt;We have to keep track of the total angle so far and add the current angle produced by population[i] and use the FillPie method to draw the piechart. &lt;/p&gt; &lt;p class="ArticleText"&gt;Next, to draw the pie chart, we make use of the following code:&lt;/p&gt; &lt;p class="ArticleText"&gt; &lt;span style="color: green;"&gt; '   Variables declaration&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt; Dim&lt;/span&gt;&lt;span style=""&gt;  i &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt; Dim&lt;/span&gt;&lt;span style=""&gt;  total &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt; Dim&lt;/span&gt;&lt;span style=""&gt;  percentage &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Double&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt; Dim&lt;/span&gt;&lt;span style=""&gt;  angleSoFar &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Double&lt;/span&gt;  = 0.0&lt;/span&gt;&lt;br /&gt; &lt;span style=""&gt; &lt;/span&gt;&lt;br /&gt; &lt;span style="color: green;"&gt; '   Caculates the total&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt; For&lt;/span&gt;&lt;span style=""&gt;  i = 0 &lt;span style="color: blue;"&gt;To&lt;/span&gt; p.Length - 1&lt;/span&gt;&lt;br /&gt; &lt;span style=""&gt;    total  += p(i)&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt; Next&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;  &lt;/span&gt;&lt;br /&gt; &lt;span style="color: green;"&gt; '   Draws the pie chart&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt; For&lt;/span&gt;&lt;span style=""&gt;  i = 0 &lt;span style="color: blue;"&gt;To&lt;/span&gt; p.Length - 1&lt;/span&gt;&lt;br /&gt; &lt;span style=""&gt;     percentage = p(i) / total * 360&lt;/span&gt;&lt;br /&gt; &lt;span style=""&gt; &lt;/span&gt;&lt;br /&gt; &lt;span style=""&gt;     g.FillPie(myBrushes(i), 25, 25, 250, 250, &lt;span style="color: blue;"&gt;CInt&lt;/span&gt;(angleSoFar), &lt;span style="color: blue;"&gt;CInt&lt;/span&gt;(percentage))&lt;/span&gt;&lt;br /&gt; &lt;span style=""&gt; &lt;/span&gt;&lt;br /&gt; &lt;span style=""&gt;     angleSoFar += percentage&lt;/span&gt;&lt;br /&gt; &lt;span style=""&gt; &lt;/span&gt;&lt;br /&gt; &lt;span style=""&gt;    &lt;span style="color: green;"&gt;'   Draws the lengend&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span style=""&gt;     g.FillRectangle(myBrushes(i), 350, 25 + (i * 50), 25, 25)&lt;/span&gt;&lt;br /&gt; &lt;span style=""&gt; &lt;/span&gt;&lt;br /&gt; &lt;span style=""&gt;    &lt;span style="color: green;"&gt;'   Label the towns&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span style=""&gt;     g.DrawString(&lt;span style="color: maroon;"&gt;"Town "&lt;/span&gt; &amp;amp; towns(i), &lt;span style="color: blue;"&gt;New&lt;/span&gt; Font(&lt;span style="color: maroon;"&gt;"Verdana"&lt;/span&gt;,  8, FontStyle.Bold), Brushes.Brown, 390, 25 + (i * 50) + 10)&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;Next&lt;/span&gt; &lt;/p&gt;&lt;p class="ArticleText"&gt;The above code has to be linked with the â€œPiechartâ€ buttonâ€™s Click event. &lt;/p&gt;&lt;p class="ArticleText"&gt; It is important to place all your codes in Try..Catch block as a good programming practice. &lt;/p&gt;&lt;p class="ArticleText"&gt;Now that we have the methods required to draw the charts, we should now output them on the browser. The piece of code below simply outputs the chart: &lt;/p&gt;  &lt;p class="ArticleText"&gt;&lt;span style="color: green;"&gt; ' Render the image to the HTML output stream.&lt;/span&gt;&lt;br /&gt;&lt;span&gt; myImage.Save(Response.OutputStream, _&lt;br /&gt;System.Drawing.Imaging.ImageFormat.Jpeg)&lt;/span&gt;&lt;/p&gt;  &lt;p class="ArticleText"&gt;Remember that myImage is the Bitmap object that we created earlier. &lt;/p&gt;&lt;p class="ArticleText"&gt;Et voila! Now, you can play around with the set of methods to produce other drawings or to improve the classes. You can also explore the rich set of methods and classes that the .NET framework provides us for producing drawings. &lt;/p&gt; &lt;p&gt;Note that every new feature will take time. If your projects requirements demands a lot of working hours, you can consider some more professional solution, like &lt;a rel="nofollow" target="_top" href="http://www.beansoftware.com/Visit/ChartingControl.Net/"&gt; ChartingControl.NET&lt;/a&gt;. This control will give you different chart types including 3D charts. However, every GDI+ solution will return you a static chart image. If you want to REALLY impress your clients or users you must have &lt;a rel="nofollow" target="_top" href="http://www.beansoftware.com/Visit/RichChartServer/"&gt;Rich Chart Server for .NET&lt;/a&gt;. On server side you work with ASP.NET control like with every other control, but it produces beautiful and interactive Flash charts on client side. It provides animations, links, interaction with clients and even audio narration. Check examples on &lt;a rel="nofollow" target="_top" href="http://www.beansoftware.com/Visit/RichChartServer/ChartGallery/"&gt;chart gallery&lt;/a&gt; to see what I am talking about, it is simply amazing!&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2770957963577646170-4732616956274890944?l=ravi-testingblog.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ravi-testingblog.blogspot.com/feeds/4732616956274890944/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ravi-testingblog.blogspot.com/2009/07/make-charts-in-aspnet-20.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2770957963577646170/posts/default/4732616956274890944'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2770957963577646170/posts/default/4732616956274890944'/><link rel='alternate' type='text/html' href='http://ravi-testingblog.blogspot.com/2009/07/make-charts-in-aspnet-20.html' title='Make Charts in ASP.NET 2.0'/><author><name>ravi</name><uri>http://www.blogger.com/profile/07517056757956161890</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2770957963577646170.post-2219455110950192705</id><published>2009-07-28T10:38:00.000-07:00</published><updated>2009-07-28T10:45:54.142-07:00</updated><title type='text'>Application Level Error Handling in ASP.NET</title><content type='html'>ASP.NET provides many different ways for error handling. Error handling starts on page level with try-catch blocks and Page_Error procedure. To find out more about this see &lt;a href="http://www.beansoftware.com/ASP.NET-Tutorials/Handling-Errors-Exceptions.aspx"&gt;Errors and Exceptions in ASP.NET&lt;/a&gt; tutorial. This tutorial goes one step further and explains error handling on Application level.      This includes handling errors with Application_Error procedure in Global.asax or by      using custom http module. These methods are usually used to log error details in      text file, database or Windows EventLog, or to send notification e-mails to      administrator.&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Handling errors in Application_Error in Global.asax&lt;/h2&gt; &lt;p&gt;Global.asax is optional file. Your site can work without it, but it could be very      useful. You can have only one Global.asax in the root      folder of your web site. Global.asax contains Application_Error procedure which      executes whenever some unhandled error occurs. By using Application_Error, you      can &lt;strong&gt;catch all unhandled errors produced by your web site&lt;/strong&gt;, and then write a code to save error      messages to database, text file or Windows EventLog, send notificationn e-mail, write some message to user, redirect user      to other page with Response.Redirect etc. If you used Server.ClearError() in      Page_Error procedure, Application_Error in Global.asax will not execute. Implementation code is      similar to Page_Error code above:&lt;/p&gt; &lt;p&gt;&lt;b&gt;[ C# ]&lt;/b&gt;&lt;/p&gt; &lt;p&gt;   &lt;span style="color: blue;"&gt;     void&lt;/span&gt;&lt;span&gt;      Application_Error(&lt;span style="color: blue;"&gt;object&lt;/span&gt; sender,     &lt;span style="color: rgb(43, 145, 175);"&gt;EventArgs&lt;/span&gt; e) &lt;/span&gt;&lt;br /&gt;  &lt;span&gt;     {&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;       &lt;span style="color: green;"&gt;// Get      current exception &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span&gt;       &lt;span style="color: rgb(43, 145, 175);"&gt;     Exception&lt;/span&gt; CurrentException = Server.GetLastError();&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;       &lt;span style="color: blue;"&gt;string&lt;/span&gt;      ErrorDetails = CurrentException.ToString();&lt;/span&gt;433333&lt;br /&gt;    &lt;span&gt;      &lt;/span&gt;&lt;br /&gt;    &lt;span&gt;       &lt;span style="color: green;"&gt;// Now do      something useful, like write error log&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;       &lt;span style="color: green;"&gt;// or      redirect a user to other page &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span&gt;       ...&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;b&gt;[ VB.NET ]&lt;/b&gt;&lt;/p&gt; &lt;p&gt;  &lt;span style="color: blue;"&gt;     Sub&lt;/span&gt;&lt;span&gt;      Application_Error(&lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; sender     &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Object&lt;/span&gt;,     &lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; e &lt;span style="color: blue;"&gt;As&lt;/span&gt;      EventArgs)&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;       &lt;span style="color: green;"&gt;' Get      current exception &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span&gt;       &lt;span style="color: blue;"&gt;Dim&lt;/span&gt;      CurrentException &lt;span style="color: blue;"&gt;As&lt;/span&gt; Exception =      Server.GetLastError()&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;       &lt;span style="color: blue;"&gt;Dim&lt;/span&gt;      ErrorDetails &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;String&lt;/span&gt;      = CurrentException.ToString()&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;      &lt;/span&gt;&lt;br /&gt;    &lt;span&gt;       &lt;span style="color: green;"&gt;' Now do      something useful, like write error log&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;       &lt;span style="color: green;"&gt;' or      redirect a user to other page &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;       ...&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;      &lt;/span&gt;&lt;br /&gt;    &lt;span style="color: blue;"&gt;End&lt;/span&gt;&lt;span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt; &lt;/p&gt;    &lt;h2&gt;Writing errors to EventLog&lt;/h2&gt; &lt;p&gt;One of the ways to track errors is to write them to EventLog. If your web site is on shared hosting you probably can't write to EventLog because of security issues. If you are on dedicated server, you need to enable "Full control" to EventLog for ASPNET account. You can do this in Registry Editor. Go to Start menu -&gt; Run... and type regedt32 or regedit and press Enter. Registry Editor will show. Go to HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\EventLog\, right mouse click and select Permissions... from context menu, like in image bellow.&lt;/p&gt; &lt;div align="center"&gt;&lt;img src="http://www.beansoftware.com/ASP.NET-Tutorials/Images/Error-Registry-Editor.gif" alt="Registry editor" border="0" /&gt;&lt;br /&gt;Registry editor&lt;/div&gt; &lt;p&gt;Click on Permissions... item to show a dialog. Give Full Control to your ASP.NET Machine Account, like in next image (include child objects too!).&lt;/p&gt; &lt;div align="center"&gt;&lt;img src="http://www.beansoftware.com/ASP.NET-Tutorials/Images/Error-Registry-Permissions.gif" alt="Registry permissions dialog" border="0" /&gt;&lt;br /&gt;Registry permissions dialog&lt;/div&gt; &lt;p&gt;Now ASPNET account has enough rights. To write in EventLog we can use Try...Catch syntax or Page_Error event, but to avoid duplication of code better option is to use Application_Error in Global.asax file. Code inside Application_Event procedure will be executed every time when some error occurs on web site.&lt;/p&gt; &lt;p&gt;&lt;b&gt;[ C# ]&lt;/b&gt;&lt;/p&gt; &lt;p&gt;   &lt;span style="color: blue;"&gt;     void&lt;/span&gt;&lt;span&gt;      Application_Error(&lt;span style="color: blue;"&gt;object&lt;/span&gt; sender,     &lt;span style="color: rgb(43, 145, 175);"&gt;EventArgs&lt;/span&gt; e) &lt;/span&gt;&lt;br /&gt;  &lt;span&gt;     {&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;       &lt;span style="color: green;"&gt;// Get      current exception &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;       &lt;span style="color: rgb(43, 145, 175);"&gt;     Exception&lt;/span&gt; CurrentException = Server.GetLastError();&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;      &lt;/span&gt;&lt;br /&gt;    &lt;span&gt;       &lt;span style="color: green;"&gt;// Name      of the log&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;       &lt;span style="color: blue;"&gt;string&lt;/span&gt;      SiteLogName = &lt;span style="color: rgb(163, 21, 21);"&gt;"My Web Site Errors"&lt;/span&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;    &lt;/span&gt;&lt;br /&gt;    &lt;span&gt;       &lt;span style="color: green;"&gt;// Check      if this log name already exists&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;       &lt;span style="color: blue;"&gt;if&lt;/span&gt;      (&lt;span style="color: rgb(43, 145, 175);"&gt;EventLog&lt;/span&gt;.SourceExists(SiteLogName) ==     &lt;span style="color: blue;"&gt;false&lt;/span&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;       {&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;              &lt;span style="color: rgb(43, 145, 175);"&gt;EventLog&lt;/span&gt;.CreateEventSource(SiteLogName,      SiteLogName);&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;       }&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;      &lt;/span&gt;&lt;br /&gt;    &lt;span&gt;       &lt;span style="color: green;"&gt;// Write      new error log&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;       &lt;span style="color: rgb(43, 145, 175);"&gt;     EventLog&lt;/span&gt; NewLog = &lt;span style="color: blue;"&gt;new&lt;/span&gt;     &lt;span style="color: rgb(43, 145, 175);"&gt;EventLog&lt;/span&gt;();&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;       NewLog.Source = SiteLogName;&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;            NewLog.WriteEntry(CurrentException.ToString(), &lt;span style="color: rgb(43, 145, 175);"&gt;     EventLogEntryType&lt;/span&gt;.Error);&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;[ VB.NET ]&lt;/b&gt;&lt;/p&gt; &lt;p&gt;   &lt;span&gt;     Sub      Application_Error(&lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; sender     &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Object&lt;/span&gt;,     &lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; e &lt;span style="color: blue;"&gt;As&lt;/span&gt;      EventArgs)&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;       &lt;span style="color: green;"&gt;' Get      current exception &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;       &lt;span style="color: blue;"&gt;Dim&lt;/span&gt;      CurrentException &lt;span style="color: blue;"&gt;As&lt;/span&gt; Exception =      Server.GetLastError()&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;      &lt;/span&gt;&lt;br /&gt;    &lt;span&gt;       &lt;span style="color: green;"&gt;' Name of      the log&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;       &lt;span style="color: blue;"&gt;Dim&lt;/span&gt;      SiteLogName &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;String&lt;/span&gt;      = &lt;span style="color: rgb(163, 21, 21);"&gt;"My Web Site Errors"&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;   &lt;/span&gt;&lt;br /&gt;    &lt;span&gt;       &lt;span style="color: green;"&gt;' Check      if this log name already exists&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;       &lt;span style="color: blue;"&gt;If&lt;/span&gt;      EventLog.SourceExists(SiteLogName) = &lt;span style="color: blue;"&gt;False&lt;/span&gt;     &lt;span style="color: blue;"&gt;Then&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;         EventLog.CreateEventSource(SiteLogName,      SiteLogName)&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;       &lt;span style="color: blue;"&gt;End&lt;/span&gt;     &lt;span style="color: blue;"&gt;If&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;br /&gt;    &lt;span&gt;       &lt;span style="color: green;"&gt;' Write      new error log&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;       &lt;span style="color: blue;"&gt;Dim&lt;/span&gt;      NewLog &lt;span style="color: blue;"&gt;As&lt;/span&gt; EventLog = &lt;span style="color: blue;"&gt;     New&lt;/span&gt; EventLog()&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;       NewLog.Source = SiteLogName&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;            NewLog.WriteEntry(CurrentException.ToString(), _       &lt;/span&gt;&lt;br /&gt;  &lt;span&gt;            EventLogEntryType.Error)&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: blue;"&gt;End&lt;/span&gt;&lt;span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;/p&gt; &lt;h2&gt;Reading web site error messages from EventLog&lt;/h2&gt; &lt;p&gt;After we created procedure for writing site error messages to Windows EventLog, we need to read them too. You can read these messages by using Windows Event Viewer, located in Control Panel. Also, you can show these messages on web page in some kind of report. To read EventLog error messages with ASP.NET and show them on page you can use code like this:&lt;/p&gt; &lt;p&gt;&lt;b&gt;[ C# ]&lt;/b&gt;&lt;/p&gt; &lt;p&gt;  &lt;span style="color: blue;"&gt;  using&lt;/span&gt;&lt;span&gt;      System;&lt;/span&gt;&lt;br /&gt;  &lt;span&gt;      &lt;/span&gt;&lt;br /&gt;  &lt;span&gt;// We need these namespace to read EventLog&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: blue;"&gt;     using&lt;/span&gt;&lt;span&gt;      System.Diagnostics;&lt;/span&gt;&lt;br /&gt;  &lt;span&gt;    &lt;/span&gt;&lt;br /&gt;  &lt;span style="color: blue;"&gt;     public&lt;/span&gt;&lt;span&gt;     &lt;span style="color: blue;"&gt;partial&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt;     &lt;span style="color: rgb(43, 145, 175);"&gt;_Default&lt;/span&gt; : System.Web.UI.&lt;span style="color: rgb(43, 145, 175);"&gt;Page&lt;/span&gt; &lt;/span&gt;&lt;br /&gt;  &lt;span&gt;     {&lt;/span&gt;&lt;br /&gt;  &lt;span&gt;       &lt;span style="color: blue;"&gt;protected&lt;/span&gt;   &lt;span style="color: blue;"&gt;void&lt;/span&gt; Page_Load(&lt;span style="color: blue;"&gt;object&lt;/span&gt;      sender, &lt;span style="color: rgb(43, 145, 175);"&gt;EventArgs&lt;/span&gt; e)&lt;/span&gt;&lt;br /&gt;  &lt;span&gt;       {&lt;/span&gt;&lt;br /&gt;  &lt;span&gt;            &lt;span style="color: green;"&gt;// Load errors to EventLog object&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span&gt;            &lt;span style="color: rgb(43, 145, 175);"&gt;EventLog&lt;/span&gt; MySiteErrorLogs =   &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;EventLog&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"My Web Site Errors"&lt;/span&gt;);&lt;/span&gt;&lt;br /&gt;  &lt;span&gt;      &lt;/span&gt;&lt;br /&gt;  &lt;span&gt;            &lt;span style="color: blue;"&gt;foreach&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;EventLogEntry&lt;/span&gt;      SiteErrorLog &lt;span style="color: blue;"&gt;in&lt;/span&gt; MySiteErrorLogs.Entries)&lt;/span&gt;&lt;br /&gt;  &lt;span&gt;         {&lt;/span&gt;&lt;br /&gt;  &lt;span&gt;              &lt;span style="color: green;"&gt;// Find when error occured&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span&gt;                Response.Write(&lt;span style="color: rgb(163, 21, 21);"&gt;"Time generated: "&lt;/span&gt; +&lt;/span&gt;&lt;br /&gt;  &lt;span&gt;                  SiteErrorLog.TimeGenerated.ToString() + &lt;span style="color: rgb(163, 21, 21);"&gt;"&amp;lt;br /&amp;gt;"&lt;/span&gt;);&lt;/span&gt;&lt;br /&gt;  &lt;span&gt;            &lt;span style="color: green;"&gt;// Show error details&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span&gt;              Response.Write(SiteErrorLog.Message +    &lt;span style="color: rgb(163, 21, 21);"&gt;"&amp;lt;hr /&amp;gt;"&lt;/span&gt;);&lt;/span&gt;&lt;br /&gt;  &lt;span&gt;         }&lt;/span&gt;&lt;br /&gt;  &lt;span&gt;       }&lt;/span&gt;&lt;br /&gt;  &lt;span&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;[ VB.NET ]&lt;/b&gt;&lt;/p&gt; &lt;p&gt;   &lt;span style="color: blue;"&gt;     Imports&lt;/span&gt;&lt;span&gt;      System&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;      &lt;/span&gt;&lt;br /&gt;  &lt;span style="color: green;"&gt;' We need these namespace to read EventLog&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: blue;"&gt;   Imports&lt;/span&gt;&lt;span&gt;      System.Diagnostics&lt;/span&gt;&lt;br /&gt;  &lt;span&gt;      &lt;/span&gt;&lt;br /&gt;  &lt;span style="color: blue;"&gt;   Partial&lt;/span&gt;&lt;span&gt;   &lt;span style="color: blue;"&gt;Class&lt;/span&gt; DefaultVB&lt;/span&gt;&lt;br /&gt;  &lt;span&gt;       &lt;span style="color: blue;"&gt;Inherits&lt;/span&gt;      System.Web.UI.Page&lt;/span&gt;&lt;br /&gt;  &lt;span&gt;    &lt;/span&gt;&lt;br /&gt;  &lt;span&gt;      &lt;span style="color: blue;"&gt;Protected&lt;/span&gt;     &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Page_Load(&lt;span style="color: blue;"&gt;ByVal&lt;/span&gt;      sender &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Object&lt;/span&gt;,     &lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; e &lt;span style="color: blue;"&gt;As&lt;/span&gt;      System.EventArgs) &lt;span style="color: blue;"&gt;Handles&lt;/span&gt;     &lt;span style="color: blue;"&gt;Me&lt;/span&gt;.Load&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;        &lt;span style="color: green;"&gt;'      Load errors to EventLog object&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;        &lt;span style="color: blue;"&gt;Dim&lt;/span&gt;      MySiteErrorLogs &lt;span style="color: blue;"&gt;As&lt;/span&gt; EventLog =     &lt;span style="color: blue;"&gt;New&lt;/span&gt; EventLog(&lt;span style="color: rgb(163, 21, 21);"&gt;"My Web      Site Errors"&lt;/span&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;      &lt;/span&gt;&lt;br /&gt;    &lt;span&gt;        &lt;span style="color: blue;"&gt;For&lt;/span&gt;     &lt;span style="color: blue;"&gt;Each&lt;/span&gt; SiteErrorLog &lt;span style="color: blue;"&gt;In&lt;/span&gt;      MySiteErrorLogs.Entries&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;              &lt;span style="color: green;"&gt;' Find when error occured&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;         Response.Write(&lt;span style="color: rgb(163, 21, 21);"&gt;"Time generated: "&lt;/span&gt; &amp;amp; _&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;                 SiteErrorLog.TimeGenerated.ToString() &amp;amp; &lt;span style="color: rgb(163, 21, 21);"&gt;"&amp;lt;br /&amp;gt;"&lt;/span&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;              &lt;span style="color: green;"&gt;' Show error details&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;              Response.Write(SiteErrorLog.Message + &lt;span style="color: rgb(163, 21, 21);"&gt;"&amp;lt;hr /&amp;gt;"&lt;/span&gt;)&lt;/span&gt;&lt;br /&gt;  &lt;span&gt;        &lt;span style="color: blue;"&gt;Next&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span&gt;      &lt;span style="color: blue;"&gt;End&lt;/span&gt;   &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: blue;"&gt;     End&lt;/span&gt;&lt;span&gt;   &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;h2&gt;Logging of unhandled errors to text file or database&lt;/h2&gt; &lt;p&gt;On the same way, we can use Application_Error in Global.asax to write error message to text file or      in some table in database.      In this example, Application_Error procedure contains a code that writes error information to      .txt file.  &lt;/p&gt; &lt;p&gt;&lt;b&gt;[ C# ]&lt;/b&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="color: blue;"&gt;void&lt;/span&gt;&lt;span style=""&gt; Application_Error(&lt;span style="color: blue;"&gt;object&lt;/span&gt; sender, &lt;span style="color: rgb(43, 145, 175);"&gt;EventArgs&lt;/span&gt; e)&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;  &lt;span style="color: green;"&gt;// Get current  exception &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;  &lt;span style="color: rgb(43, 145, 175);"&gt;Exception&lt;/span&gt; CurrentException;&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;  CurrentException = Server.GetLastError();&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;  &lt;span style="color: green;"&gt;// Write error to  text file&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;  &lt;span style="color: blue;"&gt;try&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;  {&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;    &lt;span style="color: blue;"&gt;string&lt;/span&gt; LogFilePath = Server.MapPath(&lt;span style="color: rgb(163, 21, 21);"&gt;"ErrorLog.txt"&lt;/span&gt;);&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;    &lt;span style="color: rgb(43, 145, 175);"&gt;StreamWriter&lt;/span&gt; sw = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;StreamWriter&lt;/span&gt;(LogFilePath);&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;    &lt;span style="color: green;"&gt;// Write error to  text file&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;    sw.WriteLine(CurrentException.ToString());&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;    sw.Close();&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;  }&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;  &lt;span style="color: blue;"&gt;catch&lt;/span&gt; (&lt;span style="color: rgb(43, 145, 175);"&gt;Exception&lt;/span&gt; ex)&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;  {&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;    &lt;span style="color: green;"&gt;// There could be  a problem when writing to text file&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;  }&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;b&gt;[ VB.NET ]&lt;/b&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;span style=""&gt; Application_Error(&lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; sender &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Object&lt;/span&gt;, &lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; e &lt;span style="color: blue;"&gt;As&lt;/span&gt; EventArgs)&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;  &lt;span style="color: green;"&gt;' Get current  exception &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;  &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; CurrentException &lt;span style="color: blue;"&gt;As&lt;/span&gt; Exception&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;  CurrentException = Server.GetLastError()&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;  &lt;span style="color: green;"&gt;' Write error to text  file&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;  &lt;span style="color: blue;"&gt;Try&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;    &lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;    &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; LogFilePath &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;String&lt;/span&gt; = Server.MapPath(&lt;span style="color: rgb(163, 21, 21);"&gt;"ErrorLog.txt"&lt;/span&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;       &lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;    &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; sw &lt;span style="color: blue;"&gt;As&lt;/span&gt; System.IO.StreamWriter = _&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;      &lt;span style="color: blue;"&gt;New&lt;/span&gt; System.IO.StreamWriter(LogFilePath)&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;    &lt;span style="color: green;"&gt;' Write error to text file&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;    sw.WriteLine(CurrentException.ToString())&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;    sw.Close()&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;  &lt;span style="color: blue;"&gt;Catch&lt;/span&gt; ex &lt;span style="color: blue;"&gt;As&lt;/span&gt; Exception&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;    &lt;span style="color: green;"&gt;' There could be a problem when writing to text file&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;  &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Try&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt;&lt;span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;h2&gt;Better error logging with Log4Net&lt;/h2&gt; &lt;p&gt;If you simply write error details to text file you will get simple, but not      scalable solution. In case of large number of concurrent users, text file could      be locked for writing and you will get another error. Log4Net is more scalable      solution for this problem. To find out how to use Log4Net see &lt;a href="http://www.beansoftware.com/ASP.NET-Tutorials/NHibernate-Log4Net.aspx"&gt;Using NHibernate and Log4Net in ASP.NET 2.0 applications&lt;/a&gt; tutorial. &lt;/p&gt;      &lt;h2&gt;How to send e-mail with error details&lt;/h2&gt;  &lt;p&gt;You can place a procedure for sending an email inside Application_Error,      try-catch block or any other error handling code. Function to send email is      pretty simple. In this example, code for sending notification e-mails is located      in Application_Error procedure in Global.asax file. On this way e-mail will be      sent whenever some undhandled error occurs.&lt;/p&gt; &lt;p&gt;&lt;b&gt;[ C# ]&lt;/b&gt;&lt;/p&gt; &lt;p&gt;  &lt;span style="background: yellow none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;     &amp;lt;%&lt;/span&gt;&lt;span style="color: blue;"&gt;@&lt;/span&gt;&lt;span&gt; &lt;span style="color: rgb(163, 21, 21);"&gt;     Application&lt;/span&gt; &lt;span style="color: red;"&gt;Language&lt;/span&gt;&lt;span style="color: blue;"&gt;="C#"&lt;/span&gt;     &lt;span style="background: yellow none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;%&lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="background: yellow none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;    &lt;br /&gt;    &lt;/span&gt;&lt;span style="background: yellow none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background: yellow none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;%&lt;/span&gt;&lt;span style="color: green;"&gt;-- We need these namespaces to      send e-mail --&lt;/span&gt;&lt;span style="background: yellow none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;%&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="background: yellow none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background: yellow none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;%&lt;/span&gt;&lt;span style="color: blue;"&gt;@&lt;/span&gt;&lt;span&gt; &lt;span style="color: rgb(163, 21, 21);"&gt;     Import&lt;/span&gt; &lt;span style="color: red;"&gt;Namespace&lt;/span&gt;&lt;span style="color: blue;"&gt;="System.Net"&lt;/span&gt;     &lt;span style="background: yellow none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;%&lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="background: yellow none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background: yellow none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;%&lt;/span&gt;&lt;span style="color: blue;"&gt;@&lt;/span&gt;&lt;span&gt; &lt;span style="color: rgb(163, 21, 21);"&gt;     Import&lt;/span&gt; &lt;span style="color: red;"&gt;Namespace&lt;/span&gt;&lt;span style="color: blue;"&gt;="System.Net.Mail"&lt;/span&gt;     &lt;span style="background: yellow none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;%&lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;   &lt;br /&gt;    &lt;span style="background: yellow none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;script&lt;/span&gt;&lt;span&gt; &lt;span style="color: red;"&gt;runat&lt;/span&gt;&lt;span style="color: blue;"&gt;="server"&lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: blue;"&gt;      &lt;/span&gt;&lt;br /&gt;    &lt;span style="color: blue;"&gt;     void&lt;/span&gt;&lt;span&gt;      Application_Error(&lt;span style="color: blue;"&gt;object&lt;/span&gt; sender,     &lt;span style="color: rgb(43, 145, 175);"&gt;EventArgs&lt;/span&gt; e)&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;     {&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;              &lt;span style="color: green;"&gt;// Get current exception &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;              &lt;span style="color: rgb(43, 145, 175);"&gt;Exception&lt;/span&gt; CurrentException =      Server.GetLastError();&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;              &lt;span style="color: blue;"&gt;string&lt;/span&gt; ErrorDetails =      CurrentException.ToString();&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;      &lt;/span&gt;&lt;br /&gt;    &lt;span&gt;              &lt;span style="color: green;"&gt;// Send notification e-mail&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;              &lt;span style="color: rgb(43, 145, 175);"&gt;MailMessage&lt;/span&gt; Email = &lt;/span&gt;&lt;br /&gt;    &lt;span&gt;                  &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;MailMessage&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"admin@yoursite.com"&lt;/span&gt;,&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;                  &lt;span style="color: rgb(163, 21, 21);"&gt;"admin@yoursite.com"&lt;/span&gt;);&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;         Email.IsBodyHtml =     &lt;span style="color: blue;"&gt;false&lt;/span&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;         Email.Subject =     &lt;span style="color: rgb(163, 21, 21);"&gt;"WEB SITE ERROR"&lt;/span&gt;;&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;         Email.Body =      ErrorDetails;&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;         Email.Priority =     &lt;span style="color: rgb(43, 145, 175);"&gt;MailPriority&lt;/span&gt;.High;&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;              &lt;span style="color: rgb(43, 145, 175);"&gt;SmtpClient&lt;/span&gt; sc = &lt;span style="color: blue;"&gt;new&lt;/span&gt;     &lt;span style="color: rgb(43, 145, 175);"&gt;SmtpClient&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"localhost"&lt;/span&gt;);&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;         sc.Credentials =      &lt;/span&gt;&lt;br /&gt;    &lt;span&gt;                  &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;     NetworkCredential&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"EMailAccount"&lt;/span&gt;,     &lt;span style="color: rgb(163, 21, 21);"&gt;"Password"&lt;/span&gt;);&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;         sc.Send(Email);&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;     }    &lt;/span&gt;&lt;br /&gt;    &lt;span&gt;       &lt;/span&gt;&lt;br /&gt;    &lt;span style="background: yellow none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: blue;"&gt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;script&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;[ VB.NET ]&lt;/b&gt;&lt;/p&gt; &lt;p&gt;   &lt;span style="background: yellow none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background: yellow none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;%&lt;/span&gt;&lt;span style="color: blue;"&gt;@&lt;/span&gt;&lt;span&gt; &lt;span style="color: rgb(163, 21, 21);"&gt;     Application&lt;/span&gt; &lt;span style="color: red;"&gt;Language&lt;/span&gt;&lt;span style="color: blue;"&gt;="VB"&lt;/span&gt;     &lt;span style="background: yellow none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;%&lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;      &lt;/span&gt;&lt;br /&gt;    &lt;span style="background: yellow none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background: yellow none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;%&lt;/span&gt;&lt;span style="color: green;"&gt;-- We need these namespaces to      send e-mail --&lt;/span&gt;&lt;span style="background: yellow none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;%&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="background: yellow none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background: yellow none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;%&lt;/span&gt;&lt;span style="color: blue;"&gt;@&lt;/span&gt;&lt;span&gt; &lt;span style="color: rgb(163, 21, 21);"&gt;     Import&lt;/span&gt; &lt;span style="color: red;"&gt;Namespace&lt;/span&gt;&lt;span style="color: blue;"&gt;="System.Net"&lt;/span&gt;     &lt;span style="background: yellow none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;%&lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="background: yellow none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;     &amp;lt;%&lt;/span&gt;&lt;span style="color: blue;"&gt;@&lt;/span&gt;&lt;span&gt; &lt;span style="color: rgb(163, 21, 21);"&gt;     Import&lt;/span&gt; &lt;span style="color: red;"&gt;Namespace&lt;/span&gt;&lt;span style="color: blue;"&gt;="System.Net.Mail"&lt;/span&gt;     &lt;span style="background: yellow none repeat scroll 0% 0%; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;"&gt;%&lt;/span&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;      &lt;/span&gt;&lt;br /&gt;    &lt;span style="color: blue;"&gt;     &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;script&lt;/span&gt;&lt;span&gt; &lt;span style="color: red;"&gt;runat&lt;/span&gt;&lt;span style="color: blue;"&gt;="server"&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;        &lt;/span&gt;&lt;br /&gt;    &lt;span style="color: blue;"&gt;     Sub&lt;/span&gt;&lt;span&gt;      Application_Error(&lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; sender     &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Object&lt;/span&gt;,     &lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; e &lt;span style="color: blue;"&gt;As&lt;/span&gt;      EventArgs)&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;              &lt;span style="color: green;"&gt;' Get current exception &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;              &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; CurrentException &lt;span style="color: blue;"&gt;As&lt;/span&gt;      Exception = Server.GetLastError()&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;              &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; ErrorDetails &lt;span style="color: blue;"&gt;As&lt;/span&gt;     &lt;span style="color: blue;"&gt;String&lt;/span&gt; = CurrentException.ToString()&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;      &lt;/span&gt;&lt;br /&gt;    &lt;span&gt;         &lt;span style="color: green;"&gt;'      Send notification e-mail&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;         &lt;span style="color: blue;"&gt;Dim&lt;/span&gt;      Email &lt;span style="color: blue;"&gt;As&lt;/span&gt; MailMessage = _&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;                  &lt;span style="color: blue;"&gt;New&lt;/span&gt; MailMessage(&lt;span style="color: rgb(163, 21, 21);"&gt;"admin@yoursite.com"&lt;/span&gt;,      _&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;                  &lt;span style="color: rgb(163, 21, 21);"&gt;"admin@yoursite.com"&lt;/span&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;         Email.IsBodyHtml =     &lt;span style="color: blue;"&gt;False&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;         Email.Subject =     &lt;span style="color: rgb(163, 21, 21);"&gt;"WEB SITE ERROR"&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;         Email.Body =      ErrorDetails&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;         Email.Priority =      MailPriority.High&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;              &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; sc &lt;span style="color: blue;"&gt;As&lt;/span&gt;      SmtpClient = &lt;span style="color: blue;"&gt;New&lt;/span&gt; SmtpClient(&lt;span style="color: rgb(163, 21, 21);"&gt;"localhost"&lt;/span&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;         sc.Credentials = _&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;              &lt;span style="color: blue;"&gt;New&lt;/span&gt; NetworkCredential(&lt;span style="color: rgb(163, 21, 21);"&gt;"EMailAccount"&lt;/span&gt;, &lt;span style="color: rgb(163, 21, 21);"&gt;     "Password"&lt;/span&gt;)&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;         sc.Send(Email)&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: blue;"&gt;     End&lt;/span&gt;&lt;span&gt;     &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span&gt;       &lt;/span&gt;&lt;br /&gt;    &lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;script&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;       &lt;h2&gt;Handling errors with custom HttpModule&lt;/h2&gt; &lt;p&gt;Custom Http module advantage is that module doesn't require changes in your web application code. Http module will catch the same event like Application_Error procedure. To create custom Http Module, start new project in Visual Studio. Project type should be Class Library. Add code like this:&lt;/p&gt; &lt;p&gt;&lt;b&gt;[ C# ]&lt;/b&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt;&lt;span style=""&gt; System;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;using&lt;/span&gt;&lt;span style=""&gt; System.Web;&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;namespace&lt;/span&gt; CommonModules&lt;br /&gt;{&lt;br /&gt;&lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span style=""&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ErrorHandlingModule&lt;/span&gt; : &lt;span style="color: rgb(43, 145, 175);"&gt;IHttpModule&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;  &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Init(&lt;span style="color: rgb(43, 145, 175);"&gt;HttpApplication&lt;/span&gt; app)&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;  {&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;    app.Error += &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;EventHandler&lt;/span&gt;(&lt;span style="color: blue;"&gt;this&lt;/span&gt;.HandleErrors);&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;  }&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;  &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; HandleErrors(&lt;span style="color: blue;"&gt;object&lt;/span&gt; o, &lt;span style="color: rgb(43, 145, 175);"&gt;EventArgs&lt;/span&gt; e)&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;  {&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;    &lt;span style="color: green;"&gt;// Get current  exception &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;    &lt;span style="color: rgb(43, 145, 175);"&gt;Exception&lt;/span&gt; CurrentException = ((&lt;span style="color: rgb(43, 145, 175);"&gt;HttpApplication&lt;/span&gt;)o).Server.GetLastError();&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;    &lt;span style="color: green;"&gt;// Now do  something with exception with code like in examples before, &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;    &lt;span style="color: green;"&gt;// send e-mail to  administrator,&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;    &lt;span style="color: green;"&gt;// write to  windows EventLog, database or text file&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;  }&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;  &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Dispose()&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;  {&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;    &lt;span style="color: green;"&gt;// We must have  this procedure to implement IHttpModule interface&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;  }&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;} &lt;/p&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;[ VB.NET ]&lt;/b&gt;&lt;/p&gt; &lt;p&gt;&lt;span style="color: blue;"&gt;Imports&lt;/span&gt;&lt;span style=""&gt; System&lt;/span&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;Imports&lt;/span&gt;&lt;span style=""&gt; System.Web&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;Namespace&lt;/span&gt; CommonModules&lt;br /&gt;&lt;span style="color: blue;"&gt;Public&lt;/span&gt;&lt;span style=""&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt; ErrorHandlingModule&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;  &lt;span style="color: blue;"&gt;Implements&lt;/span&gt; IHttpModule&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;  &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Init(&lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; app &lt;span style="color: blue;"&gt;As&lt;/span&gt; HttpApplication)&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;    app.Error  += &lt;span style="color: blue;"&gt;New&lt;/span&gt; EventHandler(this.HandleErrors)&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;  &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;  &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; HandleErrors(&lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; o &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Object&lt;/span&gt;, &lt;span style="color: blue;"&gt;ByVal&lt;/span&gt; e &lt;span style="color: blue;"&gt;As&lt;/span&gt; EventArgs)&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;    &lt;span style="color: green;"&gt;' Get current  exception &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;    &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; CurrentException &lt;span style="color: blue;"&gt;As&lt;/span&gt; Exception = (&lt;span style="color: blue;"&gt;CType&lt;/span&gt;(o, _&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;     HttpApplication)).Server.GetLastError()&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;    &lt;span style="color: green;"&gt;' Now do  something with exception with code like in examples before, &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;    &lt;span style="color: green;"&gt;' send e-mail to  administrator,&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;    &lt;span style="color: green;"&gt;' write to  windows EventLog, database or text file&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;  &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;  &lt;span style="color: blue;"&gt;Public&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; Dispose()&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;    &lt;span style="color: green;"&gt;' We must have  this procedure to implement IHttpModule interface&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=""&gt;  &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt;&lt;span&gt; &lt;span style="color: blue;"&gt;Class&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;End Namespace&lt;/span&gt;&lt;/p&gt; &lt;p&gt;To use the module in web application, you need to add few lines in Web.Config file.&lt;/p&gt; &lt;p&gt; &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;httpModules&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: blue;"&gt;  &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;add&lt;/span&gt; &lt;span style="color: red;"&gt; name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;ErrorHandlingModule&lt;/span&gt;" &lt;span style="color: red;"&gt;type&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;CommonModules.ErrorHandlingModule, CommonModules  &lt;/span&gt;"&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&amp;lt;&lt;span style="color: blue;"&gt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;httpModules&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;More about how to create and implement custom Http modules in ASP.NET see in &lt;a href="http://www.beansoftware.com/ASP.NET-Tutorials/Create-Http-Module.aspx"&gt;How To Create Your Own Http Module&lt;/a&gt; tutorial. &lt;/p&gt; &lt;h2&gt;ASP.NET Error Handling Remarks&lt;/h2&gt; &lt;p&gt;Be careful to handle possible errors in your Application_Error procedure. This      procedure is executed when every error occurs, so Application_Error will call      itself and it is possible to go in to infinite loop. &lt;/p&gt; &lt;p&gt;Writing error logs to text file can cause a problem with concurrency if a lot of errors need to be logged simultaneously. To make this task easier, you can use Log4Net. Also, there is pretty nice complete solution called &lt;a href="http://code.google.com/p/elmah/"&gt;Elmah&lt;/a&gt; now available at Google code. &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2770957963577646170-2219455110950192705?l=ravi-testingblog.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ravi-testingblog.blogspot.com/feeds/2219455110950192705/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ravi-testingblog.blogspot.com/2009/07/application-level-error-handling-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2770957963577646170/posts/default/2219455110950192705'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2770957963577646170/posts/default/2219455110950192705'/><link rel='alternate' type='text/html' href='http://ravi-testingblog.blogspot.com/2009/07/application-level-error-handling-in.html' title='Application Level Error Handling in ASP.NET'/><author><name>ravi</name><uri>http://www.blogger.com/profile/07517056757956161890</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2770957963577646170.post-994759837742543275</id><published>2009-07-28T10:35:00.000-07:00</published><updated>2009-07-28T10:38:19.973-07:00</updated><title type='text'>How To Create Your Own Http Module</title><content type='html'>&lt;h2&gt;Introduction&lt;/h2&gt; &lt;p&gt;The purpose this tutorial is to understand what is a Http module and how to create your own Http module. Through out this tutorial I will be using C# to explain some of the concepts.&lt;/p&gt;&lt;p&gt;Reader is assumed to know how to use of Visual Studio. Has good understanding  of ASP.NET web application and last but not least know programming using .NET  base class libraries framework.&lt;/p&gt; &lt;h2&gt;What is an Http module?&lt;/h2&gt; &lt;p&gt;So what is a Http module?&lt;/p&gt; &lt;p&gt;Simply put, Http module is a piece of code that sits between the client and the  web server. All the request that are made through the web server passes through  http module.&lt;/p&gt; &lt;p&gt;Lets have a look at very high level, how the request are process by ASP.NET  framework. When a client makes a request to your web server the request comes in  the form of Http Request. The Http Request flows through series of Http Modules.  Each of the Http module have full control over the Http request. They process or  modify the request. On the request passes through all the modules it is handed  over to Http handler. Http handlers process the request and response passes  through the all Http modules in reverse order.&lt;/p&gt; &lt;p&gt;ASP.NET framework already has few Http modules such as session state,  authorization etc. You can find list of these modules in the machine.config file  under httpModules section. &lt;/p&gt; &lt;p&gt;So what we can we do with Http module?&lt;/p&gt; &lt;p&gt;If we are creating our own Http module, we can respond to either ASP.NET events  or user events in a way we want it to respond.&lt;/p&gt; &lt;h2&gt;Authoring Http Module.&lt;/h2&gt; &lt;p&gt;The following steps are followed while creating an Http module.&lt;/p&gt; &lt;ol&gt;&lt;li&gt;Implement &lt;strong&gt;IHttpModule&lt;/strong&gt;.&lt;/li&gt;&lt;li&gt;Register the events of interest in &lt;strong&gt;Init &lt;/strong&gt;method.&lt;/li&gt;&lt;li&gt;Implement &lt;strong&gt;event handlers.&lt;/strong&gt;&lt;/li&gt;&lt;li&gt;Implement &lt;strong&gt;Dispose&lt;/strong&gt; if required.&lt;/li&gt;&lt;li&gt;Register the module in &lt;strong&gt;web.config&lt;/strong&gt;.&lt;/li&gt;&lt;/ol&gt; &lt;h2&gt;Learn by example.&lt;/h2&gt; &lt;p&gt;It is always easy to understand if it is explained with example. Let us follow the same practice. Let us say I have a requirement where I need to log the requests that are coming to my web application. Later on I will use this information to analyze my web site traffic.&lt;/p&gt; &lt;h3&gt;Create RequestLoggerModule&lt;/h3&gt;  &lt;ol&gt;&lt;li&gt;Open Visual Studio. &lt;/li&gt;&lt;li&gt;Create New Class Library project and name it RequestLoggerExample.&lt;/li&gt;&lt;li&gt;Delete the Class1.css that is created by default.&lt;/li&gt;&lt;li&gt;Add a class by name RequestLoggerModule.&lt;/li&gt;&lt;li&gt;Add reference to System.Web assembly.&lt;/li&gt;&lt;li&gt;Import the name space System.Web.&lt;/li&gt;&lt;li&gt;Now make RequestLoggerModule to implement IHttpModule.&lt;/li&gt;&lt;/ol&gt; &lt;p&gt;At this stage RequestLoggerModule code should look like this&lt;/p&gt; &lt;p&gt; &lt;span style="color: blue;"&gt;using&lt;/span&gt;&lt;span&gt;  System;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;using&lt;/span&gt;&lt;span&gt;  System.Collections.Generic;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;using&lt;/span&gt;&lt;span&gt;  System.Linq;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;using&lt;/span&gt;&lt;span&gt;  System.Text;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;using&lt;/span&gt;&lt;span&gt;  System.Web;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;namespace&lt;/span&gt;&lt;span&gt;  RequestLoggerExample&lt;/span&gt;&lt;br /&gt; &lt;span&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; RequestLoggerModule:IHttpModule&lt;/span&gt;&lt;br /&gt; &lt;span&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;#region&lt;/span&gt;&lt;span&gt;  IHttpModule Members&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Dispose()&lt;/span&gt;&lt;br /&gt; &lt;span&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span&gt; &lt;/span&gt;&lt;br /&gt; &lt;span&gt;}&lt;/span&gt;&lt;br /&gt; &lt;span&gt; &lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Init(HttpApplication context)&lt;/span&gt;&lt;br /&gt; &lt;span&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span&gt; &lt;/span&gt;&lt;br /&gt; &lt;span&gt;}&lt;/span&gt;&lt;br /&gt; &lt;span&gt; &lt;/span&gt;&lt;br /&gt; &lt;span&gt; &lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;#endregion&lt;/span&gt;&lt;br /&gt; &lt;span&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span&gt;}&lt;/span&gt; &lt;/p&gt;  &lt;p&gt;The best place to log the request is the BeginRequest. In the Init      method attach a event handler to BeginRequest event of the HttpApplication as  shown below.&lt;/p&gt; &lt;p&gt;context.BeginRequest += &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;EventHandler&lt;/span&gt;(context_BeginRequest);&lt;/p&gt; &lt;p&gt;In the context_BeginRequest method trap the request stream and log it  to a file as shown below.&lt;/p&gt; &lt;p&gt;  &lt;span style="color: blue;"&gt;void&lt;/span&gt;&lt;span&gt;  context_BeginRequest(&lt;span style="color: blue;"&gt;object&lt;/span&gt; sender, &lt;span style="color: rgb(43, 145, 175);"&gt;EventArgs&lt;/span&gt; e)&lt;/span&gt;&lt;br /&gt; &lt;span&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span&gt;    &lt;span style="color: rgb(43, 145, 175);"&gt;HttpApplication&lt;/span&gt; app = sender &lt;span style="color: blue;"&gt;as&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;HttpApplication&lt;/span&gt;;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;    StreamWriter sw = &lt;span style="color: blue;"&gt;new&lt;/span&gt; StreamWriter(&lt;span style="color: rgb(163, 21, 21);"&gt;@"C:\requestLog.txt"&lt;/span&gt;);&lt;/span&gt;&lt;br /&gt; &lt;span&gt;    StreamReader  reader = &lt;span style="color: blue;"&gt;new&lt;/span&gt; StreamReader(app.Request.InputStream);&lt;/span&gt;&lt;br /&gt; &lt;span&gt;    sw.WriteLine(reader.ReadToEnd());  sw.Close();&lt;/span&gt;&lt;br /&gt;&lt;span&gt;}&lt;/span&gt; &lt;/p&gt; &lt;p&gt;Note that writing to a file will through exception if the user context under which ASP.NET is running do not have write access.  It is not a good idea to give a write permission in your real application.&lt;/p&gt; &lt;p&gt;Now your code should look like below.&lt;/p&gt; &lt;p&gt; &lt;span style="color: blue;"&gt;using&lt;/span&gt;&lt;span&gt;  System;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;using&lt;/span&gt;&lt;span&gt;  System.Collections.Generic;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;using&lt;/span&gt;&lt;span&gt;  System.Linq;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;using&lt;/span&gt;&lt;span&gt;  System.Text;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;using&lt;/span&gt;&lt;span&gt;  System.Web;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;using&lt;/span&gt;&lt;span&gt;  System.IO;&lt;/span&gt;&lt;br /&gt; &lt;span&gt; &lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;namespace&lt;/span&gt;&lt;span&gt;  RequestLoggerExample&lt;/span&gt;&lt;br /&gt; &lt;span&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; RequestLoggerModule:IHttpModule&lt;/span&gt;&lt;br /&gt; &lt;span&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;#region&lt;/span&gt;&lt;span&gt;  IHttpModule Members&lt;/span&gt;&lt;br /&gt; &lt;span&gt; &lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Dispose()&lt;/span&gt;&lt;br /&gt; &lt;span&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span&gt; &lt;/span&gt;&lt;br /&gt; &lt;span&gt;}&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;public&lt;/span&gt;&lt;span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Init(HttpApplication context)&lt;/span&gt;&lt;br /&gt; &lt;span&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span&gt;context.BeginRequest  += &lt;span style="color: blue;"&gt;new&lt;/span&gt; EventHandler(context_BeginRequest); &lt;/span&gt;&lt;br /&gt; &lt;span&gt;}&lt;/span&gt;&lt;br /&gt; &lt;span&gt; &lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt;&lt;span&gt;  context_BeginRequest(&lt;span style="color: blue;"&gt;object&lt;/span&gt; sender, EventArgs e)&lt;/span&gt;&lt;br /&gt; &lt;span&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span&gt;HttpApplication app =  sender &lt;span style="color: blue;"&gt;as&lt;/span&gt; HttpApplication;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;StreamWriter sw = &lt;span style="color: blue;"&gt;new&lt;/span&gt; StreamWriter(&lt;span style="color: rgb(163, 21, 21);"&gt;@"C:\requestLog.txt"&lt;/span&gt;);&lt;/span&gt;&lt;br /&gt; &lt;span&gt;StreamReader reader = &lt;span style="color: blue;"&gt;new&lt;/span&gt; StreamReader(app.Request.InputStream);&lt;/span&gt;&lt;br /&gt; &lt;span&gt;sw.WriteLine(reader.ReadToEnd());&lt;/span&gt;&lt;br /&gt; &lt;span&gt;sw.Close(); &lt;/span&gt;&lt;br /&gt; &lt;span&gt;}&lt;/span&gt;&lt;br /&gt; &lt;span&gt; &lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;#endregion&lt;/span&gt;&lt;br /&gt; &lt;span&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span&gt;}&lt;/span&gt; &lt;/p&gt; &lt;h2&gt;Configuring web application.&lt;/h2&gt; &lt;p&gt;We have built the Http module successfully, let us see how we can make use of the module that we have created. &lt;/p&gt; &lt;p&gt;Follow the below steps to configure your existing ASP.NET we application to use the RequestLoggerModule.&lt;/p&gt; &lt;ol&gt;&lt;li&gt;Copy RequestLoggerExample.dll to bin folder of your web application. &lt;/li&gt;&lt;li&gt;Open the web.config file of you web application.&lt;/li&gt;&lt;li&gt;Add the below shown section to your web.config under system.web node. &lt;p&gt; &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;httpModules&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;  &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;add&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt; name&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;RequestLoggerModule&lt;/span&gt;"&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;span style="color: red;"&gt;type&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;RequestLoggerExample.RequestLoggerModule,  RequestLoggerExample&lt;/span&gt;"&lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;lt;&lt;span style="color: blue;"&gt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;httpModules&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt; &lt;/p&gt; &lt;/li&gt;&lt;/ol&gt; &lt;p&gt;If you want this module to be used by all the application  that are hosted on your server, you need to add the above section to  machine.config file.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2770957963577646170-994759837742543275?l=ravi-testingblog.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ravi-testingblog.blogspot.com/feeds/994759837742543275/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ravi-testingblog.blogspot.com/2009/07/how-to-create-your-own-http-module.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2770957963577646170/posts/default/994759837742543275'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2770957963577646170/posts/default/994759837742543275'/><link rel='alternate' type='text/html' href='http://ravi-testingblog.blogspot.com/2009/07/how-to-create-your-own-http-module.html' title='How To Create Your Own Http Module'/><author><name>ravi</name><uri>http://www.blogger.com/profile/07517056757956161890</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2770957963577646170.post-8981532460491220054</id><published>2009-07-28T10:21:00.000-07:00</published><updated>2009-07-28T10:35:18.967-07:00</updated><title type='text'>Newsletter in ASP.NET - How To Create It</title><content type='html'>This tutorial intend to teach you how to create newsletter and send mass e-mails in ASP.Net.&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Because we will not send a spam e-mails to everyone, we need a registration form, so web site visitors can register to receive  newsletter.&lt;/p&gt; &lt;p&gt;First, we're going to see how to send a confirmation email, when somebody registers in a website. This is very common, suppose you want to register a new account on some online service. If you go to the registration form and fill in all the necessary data, once you submit the form, you will most probably receive an email from the company with a message saying welcome to the site, or having your personal informations such as your username, password, name, etc...&lt;/p&gt; &lt;p&gt;This tutorial will show only basics about how to send an e-mail. Detail instructions, about how to send attachments,  make HTML e-mails, possible errors etc. you can see on &lt;a href="http://www.beansoftware.com/ASP.NET-Tutorials/Send-E-Mail.aspx"&gt;E-mail  in ASP.NET&lt;/a&gt; tutorial.&lt;/p&gt; &lt;p&gt;So to do start this Newsletter project, open up visual studio 2005, and create a new project there. &lt;/p&gt; &lt;p&gt;Note, you can download complete  &lt;a href="http://www.beansoftware.com/ASP.NET-Tutorials/Examples/Newsletter.zip"&gt;Newsletter Visual Studio project&lt;/a&gt;, used in this  tutorial.&lt;/p&gt; So, call your project "Mass_Newsletter". The language we're going to cover is Visual Basic, so make sure this is chosen from  the Language List. &lt;p&gt;Design a registration form. Usually this form will be connected to a database and the values you enter will be recorded with the database, but since we're talking about the mass email in this tutorial, we are not going to cover how to save the data in the database. Make your form similar the form in the image below:&lt;/p&gt; &lt;div align="center"&gt;&lt;img src="http://www.beansoftware.com/ASP.NET-Tutorials/Images/Newsletter-Registration-Form.gif" /&gt;&lt;/div&gt; &lt;p&gt;Clearly, the sites often request more details, but this is enough informations for testing our mass-email system.&lt;br /&gt;  Next, we will need to implement how to send the email.&lt;br /&gt;  Firstly, we will need to import the "Mail" class provided by visual studio. To do so, go to your code page and add the  following:&lt;/p&gt; &lt;p&gt;&lt;span style="color: green;"&gt;' Code starts here&lt;/span&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;Imports&lt;/span&gt;  System.Web.Mail&lt;br /&gt;&lt;span style="color: green;"&gt;' Code ends here&lt;/span&gt;&lt;/p&gt; &lt;p&gt;Make sure you add it to the very top of your page, just above the declaration of the class.&lt;br /&gt;  Now double click the submit button, to handle the onClick event. When you do so, add the following code:&lt;/p&gt; &lt;p&gt;&lt;span style="color: green;"&gt;' Code  starts here&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;Try&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;br /&gt; &lt;span&gt;  &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; theMessage &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;String&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;  theMessage = &lt;span style="color: maroon;"&gt;"Thank you for subscribing in our site&amp;lt;br/&amp;gt;"&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;  theMessage += &lt;span style="color: maroon;"&gt;"This email contains your membership details. Please  keep it safe&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span style="color: maroon;"&gt;&amp;lt;br/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span style="color: maroon;"&gt;"&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;  theMessage += &lt;span style="color: maroon;"&gt;"Username: "&lt;/span&gt; + txtUsername.Text + &lt;span style="color: maroon;"&gt;"&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span style="color: maroon;"&gt;&amp;lt;br/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span style="color: maroon;"&gt;"&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;  theMessage += &lt;span style="color: maroon;"&gt;"Password: "&lt;/span&gt; + txtPassword.Text + &lt;span style="color: maroon;"&gt;"&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span style="color: maroon;"&gt;&amp;lt;br/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span style="color: maroon;"&gt;"&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;  theMessage += &lt;span style="color: maroon;"&gt;"Thanks for using our website"&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: maroon;"&gt; &lt;/span&gt;&lt;br /&gt; &lt;span&gt;  &lt;span style="color: blue;"&gt;Dim&lt;/span&gt; mailMessage &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt; MailMessage()&lt;/span&gt;&lt;br /&gt; &lt;span&gt;  mailMessage.From = &lt;span style="color: maroon;"&gt;"subscription@yourwebsite.com"&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;  mailMessage.To =  txtEmail.Text&lt;/span&gt;&lt;br /&gt; &lt;span&gt;  mailMessage.Subject  = &lt;span style="color: maroon;"&gt;"New subscriber"&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;   mailMessage.BodyFormat = MailFormat.Html&lt;/span&gt;&lt;br /&gt; &lt;span&gt;  mailMessage.Body =  theMessage&lt;/span&gt;&lt;br /&gt; &lt;span&gt;  mailMessage.Priority  = MailPriority.High&lt;/span&gt;&lt;br /&gt; &lt;span&gt;  SmtpMail.SmtpServer  = &lt;span style="color: maroon;"&gt;"mail.yourwebsite.com"&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;   SmtpMail.Send(mailMessage)&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;Catch&lt;/span&gt;&lt;span&gt;  ex &lt;span style="color: blue;"&gt;As&lt;/span&gt; Exception&lt;/span&gt;&lt;br /&gt; &lt;span&gt;   Response.Write(ex.Message)&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;End&lt;/span&gt;&lt;span&gt; &lt;span style="color: blue;"&gt;Try&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: green;"&gt;' Code  ends here&lt;/span&gt;&lt;/p&gt; &lt;p&gt;This is pretty simple, at the beginning we defined a string called "theMessage" that holds the content of the email. Note that we can use html here, and it will be displayed correctly in the email, because we specified the "BodyFormat" to be MailFormat.Html&lt;br /&gt;  Then we defined who will receive the message, and who is the sender (mailMessage.To and mailMessage.From).&lt;br /&gt;The priority method is related to the important of the email. It will be sent quicker if you have MailPriority.High, which is our case here.&lt;/p&gt; &lt;p&gt;The SmtpServer, is simply the server that we gonna use to send the email, this is usually on the form of: mail.yourwebsite.com&lt;br /&gt;Make sure also to change "mail.yourwebsite.com" with your real website, and the fields related to it, such as "subscription@yourwebsite.com", etc... &lt;/p&gt; &lt;p&gt;At this moment, you should take into consideration that, the new subscriber should be saved in the database. Because later on we are going to send a newsletter email to all the users. To do that, you should add code just before sending the email method, to save the username information in a specific table, let's call it "tbl_subscribers".&lt;/p&gt; &lt;p&gt;This project cannot be tested offline, because it needs an SMTP server, and therefore you will have to upload the project to your own server to test it out.&lt;br /&gt; &lt;br /&gt;It will also be a good idea to redirect the page to a new page, after sending the email, most likely a thank you page such as the one in the image below:&lt;/p&gt; &lt;div align="center"&gt;&lt;img src="http://www.beansoftware.com/ASP.NET-Tutorials/Images/Newsletter-Registration-Confirmation.gif" /&gt;&lt;/div&gt; &lt;p&gt;Now we are going to create a new page, for the administrator to send newsletter emails to all the users. The technique is quite the same, but we should consider every single user in our database. For this reason, create a new page and call it "Send_New_Mail.aspx". Design your page to be similar to the following image:&lt;/p&gt; &lt;div align="center"&gt;&lt;img src="http://www.beansoftware.com/ASP.NET-Tutorials/Images/Newsletter-Send.gif" /&gt;&lt;/div&gt; &lt;p&gt;I have attached a small access database with the exercice files, that contains few records for emails of people. We will use these records to send out the newsletters email.&lt;br /&gt;  First thing first, go at the begining of the page and add the following:&lt;br /&gt;  Imports System.Threading&lt;br /&gt;&lt;/p&gt; &lt;p&gt;Then create a new function like the function below:&lt;/p&gt; &lt;p&gt;&lt;span style="color: blue;"&gt;Private&lt;/span&gt;&lt;span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt; SendEmails()&lt;/span&gt;&lt;br /&gt; &lt;span&gt; &lt;/span&gt;&lt;br /&gt; &lt;span&gt;    &lt;span style="color: blue;"&gt;Try&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        &lt;span style="color: blue;"&gt;Dim&lt;/span&gt;  myConnectionString &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt; String&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        myConnectionString  = &lt;span style="color: maroon;"&gt;"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&lt;/span&gt;  &amp;amp; Context.Server.MapPath(&lt;span style="color: maroon;"&gt;"Database\"&lt;/span&gt;) &amp;amp; &lt;span style="color: maroon;"&gt;"myDB.mdb"&lt;/span&gt; &amp;amp; &lt;span style="color: maroon;"&gt;";"&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        &lt;span style="color: blue;"&gt;Dim&lt;/span&gt;  con &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt;  System.Data.OleDb.OleDbConnection&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        con.ConnectionString  = myConnectionString&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        &lt;span style="color: blue;"&gt;Dim&lt;/span&gt;  adp &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt;  System.Data.OleDb.OleDbDataAdapter(&lt;span style="color: maroon;"&gt;"select * from  tbl_subscribers"&lt;/span&gt;, con)&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        &lt;span style="color: blue;"&gt;Dim&lt;/span&gt;  dt &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt;  System.Data.DataTable&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        adp.Fill(dt)&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        &lt;span style="color: blue;"&gt;For&lt;/span&gt;  i &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;Integer&lt;/span&gt; = 0 &lt;span style="color: blue;"&gt;To&lt;/span&gt; dt.Rows.Count - 1&lt;/span&gt;&lt;br /&gt; &lt;span&gt; &lt;/span&gt;&lt;br /&gt; &lt;span&gt;        &lt;span style="color: blue;"&gt;Dim&lt;/span&gt;  mailMessage &lt;span style="color: blue;"&gt;As&lt;/span&gt; &lt;span style="color: blue;"&gt;New&lt;/span&gt;  MailMessage()&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        mailMessage.From  = &lt;span style="color: maroon;"&gt;"administrator@yourwebsite.com"&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        mailMessage.To  = dt.Rows(i).Item(&lt;span style="color: maroon;"&gt;"myEmail"&lt;/span&gt;)&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        mailMessage.Subject  = txtSubject.Text&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        mailMessage.BodyFormat  = MailFormat.Html&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        mailMessage.Body  = txtContent.Text&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        mailMessage.Priority  = MailPriority.High&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        SmtpMail.SmtpServer  = &lt;span style="color: maroon;"&gt;"mail.yourwebsite.com"&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        SmtpMail.Send(mailMessage)&lt;/span&gt;&lt;br /&gt; &lt;span&gt; &lt;/span&gt;&lt;br /&gt; &lt;span&gt;        &lt;span style="color: blue;"&gt;Next&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        lblStatus.Text  = &lt;span style="color: maroon;"&gt;"All emails sent successfully"&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span&gt;    &lt;span style="color: blue;"&gt;Catch&lt;/span&gt; ex &lt;span style="color: blue;"&gt;As&lt;/span&gt;  Exception&lt;/span&gt;&lt;br /&gt; &lt;span&gt;        Response.Write(ex.Message)&lt;/span&gt;&lt;br /&gt; &lt;span&gt;    &lt;span style="color: blue;"&gt;End&lt;/span&gt; &lt;span style="color: blue;"&gt;Try&lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;End&lt;/span&gt;&lt;span&gt; &lt;span style="color: blue;"&gt;Sub&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p&gt;We will call this function in a new thread, instead of simple call on button click. This is important because, when we try to send Newsletter emails, often could be a lot of subscribers, and the application might return a runtime timeout error, besides that, shared hosting timeout is usually 30 seconds, so it is better to go with scalable solution with threading, that can hold both small and large mail lists.&lt;/p&gt; &lt;p&gt;&lt;br /&gt;&lt;br /&gt;Now double click the "Send Email" button to handle the onClick event. Add the following Code:&lt;/p&gt; &lt;p&gt;&lt;span style="color: green;"&gt;' Code  starts here&lt;/span&gt;&lt;br /&gt; &lt;span style="color: green;"&gt; &lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;Dim&lt;/span&gt;&lt;span&gt;  trd &lt;span style="color: blue;"&gt;As&lt;/span&gt; Thread&lt;/span&gt;&lt;br /&gt; &lt;span&gt;trd = &lt;span style="color: blue;"&gt;New&lt;/span&gt; Thread(&lt;span style="color: blue;"&gt;AddressOf&lt;/span&gt;  SendEmails)&lt;/span&gt;&lt;br /&gt; &lt;span&gt;trd.Start()&lt;/span&gt;&lt;br /&gt; &lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="color: green;"&gt;'Code  ends here&lt;/span&gt;&lt;/p&gt; &lt;p&gt;Again, this is not gonna work offline. You should test it out on your own server. &lt;/p&gt; &lt;p&gt;Nothing really complicated, the trick here was, to use the same method we provided above, but instead of sending the email to a specific person, we're having a For Loop, to pass through every single email and send out that email to, besides creating the new thread at runtime.&lt;br /&gt;Also, we are reading the Mail Subject, Body and To... from the textboxes we created and our database.&lt;br /&gt;&lt;/p&gt; &lt;p&gt;You can make this application even better. With every new facility it will look more professional. If you have strong demands but don't have time to reinvent the wheel, you can get fast results with some advanced solution, like &lt;a rel="nofollow" onmouseover="self.status='http://www.xigla.com'; return true;" onmouseout="self.status='';return true;" target="_top" href="http://www.shareit.com/affiliate.html?affiliateid=62390&amp;amp;publisherid=200056701&amp;amp;target=http%3A%2F%2Fwww.xigla.com%2Fabsolutenl%2Findex.htm"&gt; Absolute Newsletter&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;I hope you enjoyed learning from this tutorial. Happy programming!&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2770957963577646170-8981532460491220054?l=ravi-testingblog.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ravi-testingblog.blogspot.com/feeds/8981532460491220054/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ravi-testingblog.blogspot.com/2009/07/newsletter-in-aspnet-how-to-create-it.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2770957963577646170/posts/default/8981532460491220054'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2770957963577646170/posts/default/8981532460491220054'/><link rel='alternate' type='text/html' href='http://ravi-testingblog.blogspot.com/2009/07/newsletter-in-aspnet-how-to-create-it.html' title='Newsletter in ASP.NET - How To Create It'/><author><name>ravi</name><uri>http://www.blogger.com/profile/07517056757956161890</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2770957963577646170.post-4367699736453060172</id><published>2009-07-28T10:04:00.000-07:00</published><updated>2009-07-28T10:21:50.842-07:00</updated><title type='text'>Globalization and Localization</title><content type='html'>Applications must be designed to accommodate users from various cultures.  Users in different parts of the world use different languages and different  formatting standards for numbers, currency, and dates. International  applications should be customizable according to the preferences of users  belonging to different nations, cultures, or regions.&lt;br /&gt;&lt;br /&gt;&lt;p&gt;An application uses the &lt;i&gt; &lt;b&gt;locale &lt;/b&gt;&lt;/i&gt;to identify the preference of a user.&lt;i&gt; The local is the  combination of a language and a country.&lt;/i&gt; For example, the locale of a user  speaking English and belonging to the United States is "en-US". The locale  includes information about the formats used for representing time and date,  symbols and conventions used for representing currency, and the character  encoding scheme being used.&lt;/p&gt; &lt;p class="ArticleText"&gt;The structure of an internationalized application is divided into two blocks:&lt;/p&gt; &lt;ul type="square"&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;&lt;i&gt;&lt;b&gt;Code Block:&lt;/b&gt;&lt;/i&gt; Contains the application code or the executable    part of an application. This block will remain the same for all locales.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;&lt;b&gt;&lt;i&gt;Data Block: &lt;/i&gt;&lt;/b&gt;Contains all the resources, such as text and    images used by the user interface (UI). This block is locale-specific, and    each locale will have one data block.&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p class="ArticleText"&gt;The process of making  an application ready for customers is called  internationalization. It includes three phases:&lt;/p&gt; &lt;ul type="square"&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;&lt;i&gt;&lt;b&gt;Globalization: &lt;/b&gt;&lt;/i&gt;Involves writing the executable code for an    application in such a way that makes it culture-neutral and language-neutral.    While incorporating globalization, you must ensure that the culture-specific    details are not hard-coded into the executable code. The primary task in this    phase is to identify the various locale-sensitive resources and to isolate    these resources from the executable code.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;&lt;i&gt;&lt;b&gt;Localizability:&lt;/b&gt;&lt;/i&gt; An application that has been globalized must    be tested to ensure that its executable code is independent of the culture and    language-specific data. This is called localizability testing. The focus of    localizability testing is to check how the application adapts itself to    different locales.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;&lt;i&gt;&lt;b&gt;Localization:&lt;/b&gt;&lt;/i&gt; Involves the customization of an application    for a specific locale. One major task in this phase is the translation of    resources identified during the globalization phase. During this phase,    various resources, such as images and text, for the designated locale are    created.&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p class="ArticleText"&gt;When designing an international application, you should ensure the following  factors:&lt;/p&gt; &lt;ul type="square"&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;&lt;i&gt;&lt;b&gt;User Interface (UI):&lt;/b&gt;&lt;/i&gt; The language used to design different    UI components, such as menu, message box, static-text holder (label), is not    fixed during design phase of the application.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;&lt;i&gt;&lt;b&gt;Information Formats:&lt;/b&gt;&lt;/i&gt; The formats of different elements, such    as currency, number, and date, are not fixed during design phase of the    application.&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p class="ArticleText"&gt;The different aspects of an application that should be taken into account  while incorporating localization are:&lt;/p&gt; &lt;ul type="square"&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;&lt;i&gt;&lt;b&gt;Formats:&lt;/b&gt;&lt;/i&gt; The formats used for displaying numbers, time,    dates and currency should be considered while incorporating localization.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;&lt;i&gt;&lt;b&gt;Graphics:&lt;/b&gt;&lt;/i&gt; The locale-sensitive graphics, such as maps,    traffic signs, and calendar should be considered while incorporating    localization.&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt; &lt;h2&gt;Implementing Globalization&lt;/h2&gt; &lt;p class="ArticleText"&gt;The &lt;i&gt;&lt;b&gt;System.Globalization&lt;/b&gt;&lt;/i&gt; namespace provides different classes  that enable you to determine locale-specific information, such as te language  and country of an application at run time. The System.Globalization namespace  includes classes that enable you to localize your application. For example, the  Calendar class contains different calendars, such as JulianCalendar and  GrgorianCalendar.&lt;/p&gt; &lt;h2&gt;Using the CultureInfo Class&lt;/h2&gt; &lt;p class="ArticleText"&gt;&lt;br /&gt;Every locale is identified by a culture. The general format of the culture is  as follows:&lt;/p&gt; &lt;p class="ArticleText"&gt;&amp;lt;Language code&amp;gt;-&amp;lt;Country/Region code&amp;gt;&lt;/p&gt; &lt;ul type="square"&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;&lt;i&gt;&lt;b&gt;Language code:&lt;/b&gt;&lt;/i&gt; Represents a two-letter representation of a    language. It is always written in lowercase. For example, the language code    for Arabic is "ar" and that for Russian is "ru".&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;&lt;i&gt;&lt;b&gt;Country/Region code:&lt;/b&gt;&lt;/i&gt; Represents a two-letter representation    of a country or a region. It is always written in uppercase. For example, the    country code for Russia is "RU" and that for France is "FR".&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p class="ArticleText"&gt;Cultures are of two types:&lt;/p&gt; &lt;ul type="square"&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;Neutral culture: Signifies a culture that is linked only to a language and    not to a country. For example, "ru" is a neutral code.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;Specific culture: Signifies a culture that is linked to both a country and    a language. For example, "ru-RU" is a specific code.&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p class="ArticleText"&gt;The following table describes some of the commonly used properties of the  CultureInfo class:&lt;/p&gt; &lt;table border="1" cellpadding="0" cellspacing="0" width="550"&gt;   &lt;tbody&gt;&lt;tr&gt;     &lt;td valign="top"&gt;     &lt;p class="ArticleText" align="center"&gt;&lt;b&gt; PROPERTY&lt;/b&gt;&lt;/p&gt;&lt;/td&gt;     &lt;td valign="top"&gt;     &lt;p class="ArticleText" align="center"&gt;&lt;b&gt;DESCRIPTION&lt;/b&gt;&lt;/p&gt;&lt;/td&gt;   &lt;/tr&gt;   &lt;tr&gt;     &lt;td valign="top"&gt;     &lt;p class="ArticleText"&gt;CurrentCulture&lt;/p&gt;&lt;/td&gt;     &lt;td valign="top"&gt;     &lt;p class="ArticleText"&gt;Returns an instance of the CultureInfo class that      represents the culture for the current thread&lt;/p&gt;&lt;/td&gt;   &lt;/tr&gt;   &lt;tr&gt;     &lt;td valign="top"&gt;      &lt;/td&gt;     &lt;td valign="top"&gt;      &lt;/td&gt;   &lt;/tr&gt;   &lt;tr&gt;     &lt;td valign="top"&gt;     &lt;p class="ArticleText"&gt;CurrentUICulture&lt;/p&gt;&lt;/td&gt;     &lt;td valign="top"&gt;     &lt;p class="ArticleText"&gt;Returns an instance of CultureInfo class that      represents the culture for culture-specific resources.&lt;/p&gt;&lt;/td&gt;   &lt;/tr&gt;   &lt;tr&gt;     &lt;td valign="top"&gt;      &lt;/td&gt;     &lt;td valign="top"&gt;      &lt;/td&gt;   &lt;/tr&gt;   &lt;tr&gt;     &lt;td valign="top"&gt;     &lt;p class="ArticleText"&gt;DisplayName&lt;/p&gt;&lt;/td&gt;     &lt;td valign="top"&gt;     &lt;p class="ArticleText"&gt;Returns the name of the culture in format: &amp;lt;Language      code&amp;gt;-&amp;lt;country/region code&amp;gt;&lt;/p&gt;&lt;/td&gt;   &lt;/tr&gt;   &lt;tr&gt;     &lt;td valign="top"&gt;       &lt;/td&gt;     &lt;td valign="top"&gt;      &lt;/td&gt;   &lt;/tr&gt;   &lt;tr&gt;     &lt;td valign="top"&gt;     &lt;p class="ArticleText"&gt;EnglishName&lt;/p&gt;&lt;/td&gt;     &lt;td valign="top"&gt;     &lt;p class="ArticleText"&gt;Returns the English name of the culture in f&amp;lt;Language      code&amp;gt;-&amp;lt;country/region code&amp;gt; format&lt;/p&gt;&lt;/td&gt;   &lt;/tr&gt;   &lt;tr&gt;     &lt;td valign="top"&gt;&lt;p class="ArticleText"&gt;    Name&lt;/p&gt;&lt;/td&gt;     &lt;td valign="top"&gt;     &lt;p class="ArticleText"&gt;Returns the name of the culture in &amp;lt;Language      code&amp;gt;-&amp;lt;country/region code&amp;gt;      format&lt;/p&gt;&lt;/td&gt;   &lt;/tr&gt;   &lt;tr&gt;     &lt;td valign="top"&gt;     &lt;p class="ArticleText"&gt;LCID&lt;/p&gt;&lt;/td&gt;     &lt;td valign="top"&gt;     &lt;p class="ArticleText"&gt;Returns the culture identifier for the instance of the CultureInfo class.      Every culture has an identifier. For e.g., 0x0407 is the identifier for      "de-DE" culture&lt;/p&gt;&lt;/td&gt;   &lt;/tr&gt; &lt;/tbody&gt;&lt;/table&gt; &lt;p class="ArticleText"&gt;The following table describes some of the commonly used methods of the  CultureInfo class:&lt;/p&gt; &lt;table border="0" cellpadding="0" cellspacing="0" width="550"&gt;   &lt;tbody&gt;&lt;tr&gt;     &lt;td valign="top" width="200"&gt;     &lt;p class="ArticleText" align="center"&gt;&lt;b&gt;METHOD&lt;/b&gt;&lt;/p&gt;&lt;/td&gt;     &lt;td valign="top"&gt;     &lt;p class="ArticleText" align="center"&gt;&lt;b&gt;DESCRIPTION&lt;/b&gt;&lt;/p&gt;&lt;/td&gt;   &lt;/tr&gt;   &lt;tr&gt;     &lt;td valign="top"&gt;     &lt;p class="ArticleText"&gt;GetCultures&lt;/p&gt;&lt;/td&gt;     &lt;td valign="top"&gt;     &lt;p class="ArticleText"&gt;Returns a list of supported cultures.&lt;/p&gt;&lt;/td&gt;   &lt;/tr&gt;   &lt;tr&gt;     &lt;td valign="top"&gt;      &lt;/td&gt;     &lt;td valign="top"&gt;      &lt;/td&gt;   &lt;/tr&gt;   &lt;tr&gt;     &lt;td valign="top"&gt;     &lt;p class="ArticleText"&gt;GetFormat&lt;/p&gt;&lt;/td&gt;     &lt;td valign="top"&gt;     &lt;p class="ArticleText"&gt;Returns an object that represents the format of a      specified type. For e.g., this method will return an object representing the      format of the date when the specified type is date/time.&lt;/p&gt;&lt;/td&gt;   &lt;/tr&gt;   &lt;tr&gt;     &lt;td valign="top"&gt;      &lt;/td&gt;     &lt;td valign="top"&gt;      &lt;/td&gt;   &lt;/tr&gt; &lt;/tbody&gt;&lt;/table&gt; &lt;p class="ArticleText"&gt;The following code snippet shows how to use the CultureInfo class:&lt;/p&gt; &lt;p class="ArticleText"&gt;CultureInfo my_CI = &lt;span style="color: blue;"&gt;new&lt;/span&gt; CultureInfo (&lt;span style="color: maroon;"&gt;"ru-RU"&lt;/span&gt;);&lt;/p&gt; &lt;p class="ArticleText"&gt;In the preceding code snippet, a new CultureInfo class instance by the name,  my_CI, is created that sets the culture of the instance to "ru-RU".&lt;/p&gt; &lt;p class="ArticleText"&gt; &lt;/p&gt; &lt;h2&gt;Working with Resources&lt;/h2&gt; &lt;p class="ArticleText"&gt;A resource is any non-executable data that is logically deployed with an  application. A resource might be displayed in an application as an error message  or as a part of the UI. Examples of resources include the strings displayed in  the UI based on the culture settings of the system or a set of images. A  separate resource file should be created for each culture for which the  application is to be customized. &lt;i&gt;&lt;b&gt;Information is stored in a resource file  in the key/value format. &lt;/b&gt;&lt;/i&gt;For example, to store the welcome message you  must assign a the message to a key. Like this:&lt;/p&gt; &lt;p class="ArticleText"&gt;WelcomeText = &lt;span style="color: maroon;"&gt;"Welcome to my homepage"&lt;/span&gt;;&lt;/p&gt; &lt;p class="ArticleText"&gt;In the preceding code snippet, WelcomeText is the key and "Welcome to my  homepage" is the value. There are specific naming conventions to follow when  naming resource files. The resource file for default culture should be  Strings.txt or &lt;i&gt;&lt;b&gt;Strings.ResX,&lt;/b&gt;&lt;/i&gt; and for any other culture &lt;b&gt;&lt;i&gt; Strings.culture-name.ResX. &lt;/i&gt;&lt;/b&gt;Where Strings represents the name of the  file. For e.g., Strings.fr-CA.resx. After resource files are created in the text  form, they need to be converted into a binary format recognized by the .NET  Framework.&lt;/p&gt; &lt;h2&gt;Generating Resource File&lt;/h2&gt; &lt;p class="ArticleText"&gt;You can use the Resource File Generator (ResGen.exe) command prompt utility  to convert a resource file in the text format into the binary format. The syntax  is as follows:&lt;/p&gt; &lt;p class="ArticleText"&gt;resgen [/compile] [inputfilename.extension] [outputfilename.extension]&lt;/p&gt; &lt;p class="ArticleText"&gt;The extension of the input filename will be .resource.&lt;/p&gt; &lt;h2&gt;Creating Resource-Only Assemblies&lt;/h2&gt; &lt;p class="ArticleText"&gt;To create a resource-only assembly, perform the following steps:&lt;/p&gt; &lt;ol&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;Create a new Visual C# project with the Empty Project template.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;From the Project menu, select the Add Existing Item option to add the    resources files to the project.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;In Solution Explorer, right-click your project and select the Properties    option. The property page opens.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;In the Output Type drop-down menu, change the output type of your project    to class Library. This will cause your assembly to be compiled as a .dll file.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;Build you application.&lt;/p&gt;&lt;/li&gt;&lt;/ol&gt; &lt;p class="ArticleText"&gt;The output will be a DLL file, which will contain all the resource files you  added.&lt;/p&gt; &lt;h2&gt;Creating Satellite Assemblies&lt;/h2&gt; &lt;p class="ArticleText"&gt;Satellite assemblies are resource-only assemblies that contain only  culture-specific resources. The name of the resources included in a satellite  assembly should follow this naming convention:&lt;/p&gt; &lt;p class="ArticleText"&gt;Filename.culture-subculture.resource&lt;/p&gt; &lt;p class="ArticleText"&gt;The name of a resource file included within a satellite assembly should  reflect the culture to which it belongs. All applications will contain one  culture-neutral main assembly, which will be loaded by default when a request is  sent to the application. The relationship between the main assembly and the  satellite assembly under the Windows platform follows a hub-and-spoke model.  When a Web application is deployed, the main assembly is always copied in the  bin folder in the application root. Satellite assemblies are copied into the  subfolders beneath the bin folder in the application root. Satellite assemblies  are copied into the subfolders beneath the bin folder based on their culture.  For example, the satellite assembly for the culture, fr, will be copied into the  bin\fr folder and that for the culture, fr-CA, will be copied into the bin\fr\CA  folder. &lt;/p&gt; &lt;p class="ArticleText"&gt;Perform the following steps to create a satellite assembly:&lt;/p&gt; &lt;ol&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;Open the Web application to which you need to add resource files.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;From the project menu, select the Add existing Item option to add the    resource files to your application. Resource files should be named according    to the naming conventions.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;From the Build menu, select the Build solution option to build your    solution.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;Culture-specific resource files are automatically compiled into satellite    assemblies and placed in an appropriate directory structure.&lt;/p&gt;&lt;/li&gt;&lt;/ol&gt; &lt;p class="ArticleText"&gt;&lt;i&gt;&lt;b&gt;The difference between resource-only and satellite assemblies is that  resource-only assemblies can contain any resource, such as images and text.  Satellite assemblies contain only culture-specific resources.&lt;/b&gt;&lt;/i&gt;&lt;/p&gt; &lt;p class="ArticleText"&gt;You can also use the &lt;b&gt;AL.exe&lt;/b&gt; utility to link resource files to your  application. The AL (Assembly Linker) is a command prompt utility shipped with  the standard version of Visual Studio .NET.&lt;/p&gt; &lt;h2&gt;Retrieving Resources at Run Time&lt;/h2&gt; &lt;p class="ArticleText"&gt;The System.Resources namespace provides different  classes, which enable you to retrieve values from resource files. This namespace  contains the ResourceManager class which manages all  the resources associated with an application.&lt;/p&gt; &lt;p class="ArticleText"&gt;&lt;i&gt;&lt;b&gt;ResourceManager Class&lt;/b&gt;&lt;/i&gt;&lt;/p&gt; &lt;p class="ArticleText"&gt;It can be used to retrieve resources from an assembly. Each instance of the ResourceManager class is associated with an assembly  that contains the resources. This class provides different constructors that can  be used according to the need. For example, when you need to retrieve resources  from an assembly, use the ResourceManager constructor, which has the following  syntax:&lt;/p&gt; &lt;p class="ArticleText"&gt;ResourceManager ( "BaseName" , "Assembly Name", Type)&lt;/p&gt; &lt;p class="ArticleText"&gt;In the preceding syntax:&lt;/p&gt; &lt;ul type="square"&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;&lt;i&gt;&lt;b&gt;BaseName:&lt;/b&gt;&lt;/i&gt; Represents the root name of the resource.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;&lt;i&gt;&lt;b&gt;Assembly Name:&lt;/b&gt;&lt;/i&gt; Represents the name of the assembly, which    contains the resource.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;&lt;i&gt;&lt;b&gt;Type&lt;/b&gt;&lt;/i&gt;: Represents the ResourceSet, which is the set of    resources for a specific culture. The resources associated with each culture    have a ResourceSet associated with them.&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p class="ArticleText"&gt;If the resources are stored in a folder instead of an assembly, you can use  the &lt;i&gt;&lt;b&gt;FileBasedResourceManager &lt;/b&gt;&lt;/i&gt;function of the ResourceManager  class. This function takes the following three parameters:&lt;/p&gt; &lt;ul type="square"&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;&lt;i&gt;&lt;b&gt;BaseName:&lt;/b&gt;&lt;/i&gt; Represents the root of the resource&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;&lt;i&gt;&lt;b&gt;ResourceDir: &lt;/b&gt;&lt;/i&gt;Represents the folder in which the resource is    to be searched.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;&lt;i&gt;&lt;b&gt;Type:&lt;/b&gt;&lt;/i&gt; Represents the ResourceSet.&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p class="ArticleText"&gt;The other important classes in the System.Resources namespace are:&lt;/p&gt; &lt;ul type="square"&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;&lt;i&gt;&lt;b&gt;ResourceWriter:&lt;/b&gt;&lt;/i&gt; Used to write into a resource file.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;&lt;i&gt;&lt;b&gt;ResourceReader: &lt;/b&gt;&lt;/i&gt;Used to read form a resource file.&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt; &lt;h2&gt;Configuring a Web application for Globalization&lt;/h2&gt; &lt;p class="ArticleText"&gt;In ASP.NET, you can use the Web.config file to configure an application for  globalization. The &amp;lt;globalization&amp;gt; tag enables you to configure locale-specific  information, such as culture and encoding. The &amp;lt;globalization&amp;gt; tag has the  following attributes:&lt;/p&gt; &lt;ul type="square"&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;requestEncoding: Allows you to specify the assumed encoding of incoming    requests. The default value of this attribute is utf-8.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;responseEncoding: Allows you to specify the encoding used for the response    data. The default value is utf-8.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;fileEncoding: Allows you to specify the way in which the .aspx, .asmx, .asax    files are encoded in a Web application. The default value is utf-8.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;culture: Allows you to specify the culture.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;uiCulture: Allows you to specify the UI culture.&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p class="ArticleText"&gt;The following code snippet shows an example of how to use the &amp;lt;globalization&amp;gt;  tag to configure a Web application:&lt;/p&gt; &lt;p class="ArticleText"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;globalization&lt;/span&gt;&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;br /&gt; &lt;span style="color: red;"&gt; requestEncoding&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;utf-8&lt;/span&gt;"&lt;span style="color: blue;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;br /&gt; &lt;span style="color: red;"&gt; responseEncoding&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;utf-8&lt;/span&gt;"&lt;/span&gt;&lt;br /&gt; &lt;span style="color: red;"&gt;culture&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;ru-RU&lt;/span&gt;"&lt;/span&gt;&lt;br /&gt;&lt;span style="color: red;"&gt;uiculture&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color: blue;"&gt;ru-RU&lt;/span&gt;"&lt;span style="color: blue;"&gt;  /&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;h2&gt;Practical Demonstration&lt;/h2&gt; &lt;p class="ArticleText"&gt; Here we will be creating an ASP.NET application that is locale-aware. For  simplicity sake we our only creating it for 2 cultures, although its just the  repetition of steps that would be required if you wish to implement using more  cultures.&lt;/p&gt; &lt;p class="ArticleText"&gt; Our application will have an user interface with a drop down list from where the  user can select the culture of his choice and then the Text values of the  controls would be as per the selected culture.&lt;/p&gt; &lt;h2&gt;Solution:&lt;/h2&gt; &lt;p class="ArticleText"&gt; Perform the following steps for the same:&lt;/p&gt; &lt;ol&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;Create a new Web application and I have named it "globalization" as per    this example.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;Change the name of the Webform1.aspx to culture.aspx.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;Design the culture form. Drag two label controls, a dropdownlist and a    button. &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;Create two resource Files. For this right-click the project file and    select the Add-&gt;New folder. Rename this folder as Resources.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;Now add new items to this project, select resource assembly file from the    template pane. Rename it as strings.resx.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;In the name column write "msg" and in the value column write "Welcome to    my homepage", in this strings.resx file. Save this file and close it.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;Drag this file to the "Resources" folder from the solution explorer.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;Similarly add another .resx file, remember to use the culture convention    of renaming it. We will be renaming it for France culture so, the it would be    renamed as "strings.fr-FR.resx".&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;Now we need to generate &lt;i&gt;&lt;b&gt;strings.Resources&lt;/b&gt;&lt;/i&gt; file and &lt;i&gt;&lt;b&gt;   strings.fr-FR.resources&lt;/b&gt;&lt;/i&gt; file.For this go to the command prompt of    Visual Studio .NET (go to start-&amp;gt;all programs-&amp;gt;Microsoft Visual Studio .net    2003-&amp;gt;Visual Studio .net tools-&amp;gt;Microsoft Visual Studio .net command prompt)&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;Type the following command there:  &lt;b&gt;resgen strings.resx&lt;/b&gt;&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;Note you will need to navigate to the "Resources" folder using " cd "    command of DOS, since you have this .resx file in Resources folder. &lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;Do this for all your .resx files and their respective .resources files    would be generated, as shown in the figure below:&lt;/p&gt;&lt;/li&gt;&lt;/ol&gt; &lt;div align="center"&gt;&lt;img src="http://www.beansoftware.com/ASP.NET-Tutorials/Images/Globalization-Localization-Resource.gif" border="0" /&gt;&lt;/div&gt; &lt;p class="ArticleText"&gt; You can see these files in your Resources folder of your application as shown in  the figure.&lt;/p&gt; &lt;div align="center"&gt;&lt;img src="http://www.beansoftware.com/ASP.NET-Tutorials/Images/Globalization-Localization-Strings.jpg" border="0" /&gt;&lt;/div&gt; &lt;p class="ArticleText"&gt; The main code performing this functionality is given below. You can download  example Visual Studio project from &lt;a href="http://www.beansoftware.com/ASP.NET-Tutorials/Examples/Globalization-Localization.zip"&gt;this link&lt;/a&gt;.&lt;/p&gt; &lt;p class="ArticleText"&gt;&lt;span style="color: blue;"&gt;private&lt;/span&gt;&lt;span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Page_Load(&lt;span style="color: blue;"&gt;object&lt;/span&gt;  sender, System.EventArgs e)&lt;/span&gt;&lt;br /&gt; &lt;span&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span&gt; System.Resources.ResourceManager rm =ResourceManager.CreateFileBasedResourceManager( &lt;span style="color: maroon;"&gt;"strings"&lt;/span&gt;, Server.MapPath(&lt;span style="color: maroon;"&gt;"Resources"&lt;/span&gt;)+Path.DirectorySeparatorChar  , &lt;span style="color: blue;"&gt;null&lt;/span&gt;);&lt;/span&gt;&lt;br /&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt;&lt;span&gt;  a = Session[&lt;span style="color: maroon;"&gt;"lang"&lt;/span&gt;].ToString ();&lt;/span&gt;&lt;br /&gt; &lt;span&gt;CultureInfo cinfo=&lt;span style="color: blue;"&gt;new&lt;/span&gt;  CultureInfo(a);&lt;/span&gt;&lt;br /&gt; &lt;span&gt;lblmsg.Text=rm.GetString  (&lt;span style="color: maroon;"&gt;"msg"&lt;/span&gt;,cinfo);&lt;/span&gt;&lt;br /&gt;&lt;span&gt;}&lt;/span&gt;&lt;/p&gt; &lt;p class="ArticleText"&gt; Here Session["lang"] is assigned the value of the selected item form the drop  down list, that contains the available cultures for the site.&lt;/p&gt; &lt;p class="ArticleText"&gt; The first output screen looks like this:&lt;/p&gt; &lt;div align="center"&gt;&lt;img src="http://www.beansoftware.com/ASP.NET-Tutorials/Images/Globalization-Localization-Select-Culture.gif" border="0" /&gt;&lt;/div&gt; &lt;p class="ArticleText"&gt;When you select any culture, the message is displayed in that particular  culture as provided by you in the .resx files (remember the name/value pairs!)&lt;/p&gt; &lt;div align="center"&gt;&lt;img src="http://www.beansoftware.com/ASP.NET-Tutorials/Images/Globalization-Localization-Language.gif" border="0" /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2770957963577646170-4367699736453060172?l=ravi-testingblog.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ravi-testingblog.blogspot.com/feeds/4367699736453060172/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ravi-testingblog.blogspot.com/2009/07/globalization-and-localization.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2770957963577646170/posts/default/4367699736453060172'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2770957963577646170/posts/default/4367699736453060172'/><link rel='alternate' type='text/html' href='http://ravi-testingblog.blogspot.com/2009/07/globalization-and-localization.html' title='Globalization and Localization'/><author><name>ravi</name><uri>http://www.blogger.com/profile/07517056757956161890</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2770957963577646170.post-358564322818457409</id><published>2009-07-28T09:56:00.000-07:00</published><updated>2009-07-28T10:03:35.018-07:00</updated><title type='text'>How to Display PDF documents with ASP.NET</title><content type='html'>&lt;span style="font-weight: bold;"&gt;Introduction&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;This article describes how to embed and display PDF documents in a webpage using simple ASP.NET custom server control. The method used, allows the developer to control the web page contents surrounding the embedded PDF.&lt;br /&gt;&lt;br /&gt;&lt;p&gt;This is not same as linking directly to a PDF which doesn't allow developer to control the appearance of the page.&lt;/p&gt; &lt;br /&gt;&lt;div align="center"&gt;&lt;p class="ArticleText"&gt;&lt;img src="http://www.beansoftware.com/ASP.NET-Tutorials/Images/Pdf-Control-Viewer.jpg" /&gt;&lt;br /&gt;Figure1. Embedding and Displaying PDF documents&lt;/p&gt;&lt;/div&gt; &lt;div align="center"&gt;&lt;p class="ArticleText"&gt;&lt;img src="http://www.beansoftware.com/ASP.NET-Tutorials/Images/Pdf-Control-Direct.jpg" /&gt;&lt;br /&gt;Figure 2. Linking Directly to a PDF&lt;/p&gt;&lt;/div&gt; &lt;p class="ArticleText"&gt;There are two Visual Studio .NET solutions included with this download,  one is &lt;a href="http://www.beansoftware.com/ASP.NET-Tutorials/Examples/Pdf-Control.zip"&gt;web custom control library&lt;/a&gt;  containing a single custom control used to render out the PDF, and  the other is a  &lt;a href="http://www.beansoftware.com/ASP.NET-Tutorials/Examples/Pdf-Test-Site.zip"&gt;test web site used to display a PDF&lt;/a&gt; through the use of the control.&lt;/p&gt; &lt;p class="ArticleText"&gt;Figure 3 (below) shows the solution explorer for the project.  The project appearing at the top of the solution is the test web site,  it contains only a single web page (default) and it includes a PDF file included for testing purposes.  The bottom project is the web custom control library with the single control included (ShowPdf).  The references in the test web site are per the default configuration;  the custom control library references include the default references but also include System.Design namespace.&lt;/p&gt; &lt;div align="center"&gt;&lt;p class="ArticleText"&gt;&lt;img src="http://www.beansoftware.com/ASP.NET-Tutorials/Images/Pdf-Control-Solution.jpg" /&gt;&lt;br /&gt;Figure 3. Solution Explorer with Both Projects Visible&lt;/p&gt;&lt;/div&gt; &lt;h2&gt;The PDF Viewer Web Custom Control Project&lt;/h2&gt; &lt;p class="ArticleText"&gt;&lt;br /&gt;&lt;br /&gt;Within the &lt;a href="http://www.beansoftware.com/ASP.NET-Tutorials/Examples/Pdf-Control.zip"&gt;web custom control  project&lt;/a&gt;,  there is a single custom control provided in this example.  The example is entitled, ShowPdf.cs.  The code for the project is very simple and should take very little time to implement.  The control code starts out with the default imports:&lt;/p&gt; &lt;p class="ArticleText"&gt;&lt;span style="color: blue;"&gt;&lt;span&gt;using&lt;/span&gt;&lt;/span&gt;&lt;span&gt; System;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;&lt;span&gt;using&lt;/span&gt;&lt;/span&gt;&lt;span&gt; System.Collections.Generic;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;&lt;span&gt;using&lt;/span&gt;&lt;/span&gt;&lt;span&gt; System.ComponentModel;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;&lt;span&gt;using&lt;/span&gt;&lt;/span&gt;&lt;span&gt; System.Text;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;&lt;span&gt;using&lt;/span&gt;&lt;/span&gt;&lt;span&gt; System.Web;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;&lt;span&gt;using&lt;/span&gt;&lt;/span&gt;&lt;span&gt; System.Web.UI;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;&lt;span&gt;using&lt;/span&gt;&lt;/span&gt;&lt;span&gt; System.Web.UI.WebControls;&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;&lt;span&gt;namespace&lt;/span&gt;&lt;/span&gt;&lt;span&gt; PdfViewer&lt;/span&gt;&lt;br /&gt;&lt;span&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span&gt;    [DefaultProperty(&lt;span style="color: purple;"&gt;"FilePath"&lt;/span&gt;)]&lt;/span&gt;&lt;br /&gt;&lt;span&gt;    [ToolboxData(&lt;span style="color: purple;"&gt;"&amp;lt;{0}:ShowPdf&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: purple;"&gt;&lt;span&gt;        runat=server&amp;gt;&amp;lt;/{0}:ShowPdf&amp;gt;"&lt;/span&gt;&lt;/span&gt;&lt;span&gt;)]&lt;/span&gt;&lt;br /&gt;&lt;span&gt;    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; ShowPdf : WebControl&lt;/span&gt;&lt;br /&gt;&lt;span&gt;    {&lt;/span&gt;&lt;/p&gt; &lt;p class="ArticleText"&gt;Following the imports is the namespace and class declaration.  The class contains a single property called FilePath, and the attributes for the class assign the default property  attribute to point to the single added property.  What this accomplishes is simple, when the control is dropped into a web page or selected by the developer at design time,  the property editor will default to select this property.  The toolbox data attribute is setup for a custom server control (runat=server).&lt;/p&gt; &lt;p class="ArticleText"&gt;After the class declaration, a declarations region was added and a single local member variable  was defined and included within that region.  The local member variable is used to retain the path to the PDF document loaded into the control.&lt;/p&gt; &lt;p class="ArticleText"&gt;&lt;span style="color: navy;"&gt;&lt;span&gt;#region&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: purple;"&gt;&lt;span&gt;"Declarations"&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span&gt;    privatestring mFilePath;&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="color: navy;"&gt;&lt;span&gt;#endregion&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="ArticleText"&gt;The next bit of code in the class is contained in a new region called "Properties".  Within this region is a single property entitled, FilePath.  The property is used to provide public member access to the file path member variable.  The attributes associated with this property indicate that the property is visible (Browsable) in the property editor,  defines the property editor category under which to show the property in the editor,  and provides the text used to describe the property which viewed in the property editor (Description).  The editor defined specifies an association between this property and the URL Editor;  when the developer using the control edits the property at design time,  the URL editor will be displayed to allow the developer to navigate to and select a target file based using this editor.  The System.Design reference is needed to support this portion of the design. &lt;/p&gt; &lt;p class="ArticleText"&gt;&lt;span style="color: navy;"&gt;&lt;span&gt;#region "Properties"&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span&gt;    [Category(&lt;span style="color: purple;"&gt;"Source File"&lt;/span&gt;)]&lt;/span&gt;&lt;br /&gt;&lt;span&gt;    [Browsable(&lt;span style="color: blue;"&gt;true&lt;/span&gt;)]&lt;/span&gt;&lt;br /&gt;&lt;span&gt;    [Description(&lt;span style="color: purple;"&gt;"Set path to source file."&lt;/span&gt;)]&lt;/span&gt;&lt;br /&gt;&lt;span&gt;    [Editor(&lt;span style="color: blue;"&gt;typeof&lt;/span&gt;(System.Web.UI.Design.UrlEditor), &lt;/span&gt;&lt;br /&gt;&lt;span&gt;        &lt;span style="color: blue;"&gt;typeof&lt;/span&gt;(System.Drawing.Design.UITypeEditor))]&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span&gt;    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt; FilePath&lt;/span&gt;&lt;br /&gt;&lt;span&gt;    {&lt;/span&gt;&lt;br /&gt;&lt;span&gt;        &lt;span style="color: blue;"&gt;get&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;        {&lt;/span&gt;&lt;br /&gt;&lt;span&gt;            &lt;span style="color: blue;"&gt;return&lt;/span&gt; mFilePath;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;        }&lt;/span&gt;&lt;br /&gt;&lt;span&gt;          &lt;/span&gt;&lt;br /&gt;&lt;span&gt;        &lt;span style="color: blue;"&gt;set&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;        {&lt;/span&gt;&lt;br /&gt;&lt;span&gt;            &lt;span style="color: blue;"&gt;if&lt;/span&gt; (value == &lt;span style="color: blue;"&gt;string&lt;/span&gt;.Empty)&lt;/span&gt;&lt;br /&gt;&lt;span&gt;            {&lt;/span&gt;&lt;br /&gt;&lt;span&gt;                mFilePath = &lt;span style="color: blue;"&gt;string&lt;/span&gt;.Empty;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;            }&lt;/span&gt;&lt;br /&gt;&lt;span&gt;            &lt;span style="color: blue;"&gt;else&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;            {&lt;/span&gt;&lt;br /&gt;&lt;span&gt;                &lt;span style="color: blue;"&gt;int&lt;/span&gt; tilde = -&lt;span style="color: midnightblue;"&gt;1&lt;/span&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;                tilde = value.IndexOf('~');&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span&gt;                &lt;span style="color: blue;"&gt;if&lt;/span&gt; (tilde != -&lt;span style="color: midnightblue;"&gt;1&lt;/span&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span&gt;                {&lt;/span&gt;&lt;br /&gt;&lt;span&gt;                    mFilePath = value.Substring((tilde+ &lt;span style="color: midnightblue;"&gt;2&lt;/span&gt;))&lt;/span&gt;&lt;br /&gt;&lt;span&gt;                                     .Trim();&lt;/span&gt;&lt;br /&gt;&lt;span&gt;                }&lt;/span&gt;&lt;br /&gt;&lt;span&gt;                &lt;span style="color: blue;"&gt;else&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;                {&lt;/span&gt;&lt;br /&gt;&lt;span&gt;                    mFilePath = value;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;                }&lt;/span&gt;&lt;br /&gt;&lt;span&gt;            }&lt;/span&gt;&lt;br /&gt;&lt;span&gt;        }&lt;/span&gt;&lt;br /&gt;&lt;span&gt;    }   &lt;span style="color: green; font-style: italic;"&gt;//&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;end FilePath property&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="color: navy;"&gt;&lt;span&gt;#endregion&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="ArticleText"&gt;Notice that in the set side of the property,  the code is written to remove the tilde from in front of the file path if the tilde is present.  If the tilde is left intact after setting the property to point to a file using the URL Editor,  the tilde would otherwise be included in the HTML rendered to the page and the file would not found.  It is necessary to strip this character from the file path in order to use the URL Editor  to set this property at design time.&lt;/p&gt; &lt;p class="ArticleText"&gt;The last bit of code needed to finish the control is contained in a region called  "Rendering". This region contains a single method used to override the RenderContents method.  Within RenderContents, a string builder is created and then populated with the HTML needed to render the control on a page.  In this instance, the simplest way to display the PDF is through the use of an IFrame.  Looking at the string builder, note that the IFrame contains the source property which points to  the file path property added earlier in this project.  Further, the width and height of the IFrame is set to equal the height and width of the control itself.  After the string builder is populated, the content is dumped into a div.  The entire control is constructed within a try catch block,  if the try fails, the catch block will render out "Display PDF Control" into a box on the page in lieu of showing the control.  When the control is first added to the page, it does not point to a file and so the try will fail,  this prevents an error from occurring during that initial placement of the control.&lt;/p&gt; &lt;p class="ArticleText"&gt;&lt;span style="color: navy;"&gt;&lt;span&gt;#region "Rendering"&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="color: blue;"&gt;&lt;span&gt;protected&lt;/span&gt;&lt;/span&gt;&lt;span&gt; &lt;span style="color: blue;"&gt;override&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; RenderContents(HtmlTextWriter&lt;/span&gt;&lt;br /&gt;&lt;span&gt;                                       writer)&lt;/span&gt;&lt;br /&gt;&lt;span&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span&gt;    &lt;span style="color: blue;"&gt;try&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;    {&lt;/span&gt;&lt;br /&gt;&lt;span&gt;        StringBuilder sb = newStringBuilder();&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span&gt;        sb.Append(&lt;span style="color: purple;"&gt;"&amp;lt;iframe src="&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;                  +FilePath.ToString() + &lt;span style="color: purple;"&gt;" "&lt;/span&gt;);&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span&gt;        sb.Append(&lt;span style="color: purple;"&gt;"width="&lt;/span&gt; +&lt;/span&gt;&lt;br /&gt;&lt;span&gt;                  Width.ToString() + &lt;span style="color: purple;"&gt;" height="&lt;/span&gt; + &lt;/span&gt;&lt;br /&gt;&lt;span&gt;                  Height.ToString() + &lt;span style="color: purple;"&gt;" "&lt;/span&gt;);&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span&gt;        sb.Append(&lt;span style="color: purple;"&gt;"&amp;lt;View PDF: &amp;lt;a&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: purple;"&gt;&lt;span&gt;            href="&lt;/span&gt;&lt;/span&gt;&lt;span&gt; + FilePath.ToString() + &lt;span style="color: purple;"&gt;"&amp;lt;/a&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: purple;"&gt;&lt;span&gt;            "&lt;/span&gt;&lt;/span&gt;&lt;span&gt;);&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span&gt;        sb.Append(&lt;span style="color: purple;"&gt;"&amp;lt;/iframe&amp;gt;"&lt;/span&gt;);&lt;/span&gt;&lt;br /&gt;&lt;span&gt;               &lt;/span&gt;&lt;br /&gt;&lt;span&gt;        writer.RenderBeginTag(HtmlTextWriterTag.Div);&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span&gt;        writer.Write(sb.ToString());&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span&gt;        writer.RenderEndTag();&lt;/span&gt;&lt;br /&gt;&lt;span&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;span&gt;    &lt;span style="color: blue;"&gt;catch&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;    {&lt;/span&gt;&lt;br /&gt;&lt;span&gt;               &lt;/span&gt;&lt;br /&gt;&lt;span&gt;        &lt;span style="color: green; font-style: italic;"&gt;// with no properties set, this will render&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;        &lt;span style="color: green; font-style: italic;"&gt;// "Display PDF Control" in &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;        &lt;span style="color: green; font-style: italic;"&gt;// a box on the page&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span&gt;        writer.RenderBeginTag(HtmlTextWriterTag.Div);&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span&gt;        writer.Write(&lt;span style="color: purple;"&gt;"Display PDF Control"&lt;/span&gt;);&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span&gt;        writer.RenderEndTag();     &lt;/span&gt;&lt;br /&gt;&lt;span&gt;    }  &lt;span style="color: green; font-style: italic;"&gt;// end try-catch&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span&gt;}   &lt;span style="color: green; font-style: italic;"&gt;//&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span&gt;end RenderContents&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color: navy;"&gt; &lt;span&gt; #endregion&lt;/span&gt;&lt;/span&gt; &lt;/p&gt; &lt;h2&gt;The PDF Viewer Test Web Project&lt;/h2&gt; &lt;p class="ArticleText"&gt;The default page included in the web project is provided to serve as a test bed for the control.  The page contains only a panel used as a banner,  a hyperlink pointing directly to a PDF file, and the custom control with its file path property also pointing to the PDF.  The PDF added to the web site content is also included in the web project.  When this site is viewed, the control will display the PDF document in the defined area,  selection of the hyperlink will open the same PDF into a separate window;  I just included the hyperlink for comparison purposes.&lt;/p&gt; &lt;h2&gt;Summary&lt;/h2&gt; &lt;p class="ArticleText"&gt;This article demonstrates an approach that may be used to develop a custom control through which PDFs  may be embedded into a web page.  The purpose of the control is to allow the PDF to be included within a web page as opposed to the alternative  of opening the PDF into a separate page where the PDF consumes the entire available display area  and where the user cannot control the appearance of that page.  Naturally, the code included in the custom control could be added directly into any page and the same effect  could be achieved, however, by adding the code once into a custom control,  the developer need only drop the control into the form and set the file path and dimensions to display PDFs  without repeating the manual addition of the code each time it is needed.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2770957963577646170-358564322818457409?l=ravi-testingblog.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ravi-testingblog.blogspot.com/feeds/358564322818457409/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ravi-testingblog.blogspot.com/2009/07/how-to-display-pdf-documents-with.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2770957963577646170/posts/default/358564322818457409'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2770957963577646170/posts/default/358564322818457409'/><link rel='alternate' type='text/html' href='http://ravi-testingblog.blogspot.com/2009/07/how-to-display-pdf-documents-with.html' title='How to Display PDF documents with ASP.NET'/><author><name>ravi</name><uri>http://www.blogger.com/profile/07517056757956161890</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2770957963577646170.post-518473059467668402</id><published>2009-07-28T09:52:00.000-07:00</published><updated>2009-07-28T09:56:25.061-07:00</updated><title type='text'>Three Tier Architecture in ASP.NET</title><content type='html'>3-tier application is a program which is organized into three major disjunctive tiers on layers. Here we can see that how these layers increase the reusability of codes. &lt;br /&gt;&lt;br /&gt;These layers are described below.&lt;br /&gt;&lt;br /&gt;1. Application layer or Business layer&lt;br /&gt;2. Business layer&lt;br /&gt;    a. Property layer(Sub layer of business layer)&lt;br /&gt;3. data layer&lt;br /&gt;Advantages of three Tier Architecture.&lt;br /&gt;&lt;br /&gt;The main characteristic of a Host Architecture is that the application and databases reside on the same host computer and the user interacts with the host using an unfriendly and dump terminal. This architecture does not support distributed computing (the host applications are not able to connect a database of a strategically allied partner). Some managers found that developing a host application take too long and it is expensive. Consequently led these disadvantages to Client-Server architecture.&lt;br /&gt;&lt;br /&gt;Client-Server architecture is 2-Tier architecture because the client does not distinguish between Presentation layer and business layer. The increasing demands on GUI controls caused difficulty to manage the mixture of source code from GUI and Business Logic (Spaghetti Code). Further, Client Server Architecture does not support enough the Change Management. Let suppose that the government increases the Entertainment tax rate from 4% to 8 %, then in the Client-Server case, we have to send an update to each clients and they must update synchronously on a specific time otherwise we may store invalid or wrong information. The Client-Server Architecture is also a burden to network traffic and resources. Let us assume that about five hundred clients are working on a data server then we will have five hundred ODBC connections and several ruffian record sets, which must be transported from the server to the clients (because the Business layer is stayed in the client side). The fact that Client-Server does not have any caching facilities like in ASP.NET, caused additional traffic in the network. Normally, a server has a better hardware than client therefore it is able compute algorithms faster than a client, so this fact is also an additional pro argument for the 3.Tier Architecture. This categorization of the application makes the function more reusable easily and it becomes too easy to find the functions which have been written previously. If programmer wants to make further update in the application then he easily can understand the previous written code and can update easily.&lt;br /&gt;Application layer or Presentation layer&lt;br /&gt;&lt;br /&gt;Application layer is the form which provides the user interface to either programmer of end user. Programmer uses this layer for designing purpose and to get or set the data back and forth.&lt;br /&gt;Business layer&lt;br /&gt;&lt;br /&gt;This layer is a class which we use to write the function which works as a mediator to transfer the data from Application or presentation layer data layer. In the three tier architecture we never let the data access layer to interact with the presentation layer.&lt;br /&gt;a. Property Layer&lt;br /&gt;&lt;br /&gt;This layer is also a class where we declare the variable corresponding to the fields of the database which can be required for the application and make the properties so that we can get or set the data using these properties into the variables. These properties are public so that we can access its values.&lt;br /&gt;Data Access Layer&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This layer is also a class which we use to get or set the data to the database back and forth. This layer only interacts with the database. We write the database queries or use stored procedures to access the data from the database or to perform any operation to the database.&lt;br /&gt;Summary&lt;br /&gt;&lt;br /&gt;    *&lt;br /&gt;&lt;br /&gt;      Application layer is the form where we design using the controls like textbox, labels, command buttons etc.&lt;br /&gt;    *&lt;br /&gt;&lt;br /&gt;      Business layer is the class where we write the functions which get the data from the application layer and passes through the data access layer.&lt;br /&gt;    *&lt;br /&gt;&lt;br /&gt;      Data layer is also the class which gets the data from the business layer and sends it to the database or gets the data from the database and sends it to the business layer.&lt;br /&gt;    *&lt;br /&gt;&lt;br /&gt;      Property layer is the sub layer of the business layer in which we make the properties to sent or get the values from the application layer. These properties help to sustain the value in a object so that we can get these values till the object destroy.&lt;br /&gt;&lt;br /&gt;Data flow from application layer to data layer&lt;br /&gt;&lt;br /&gt;You can download sample three tier project, used for this tutorial. Here we are passing the code of the student to the business layer and on the behalf of that getting the data from the database which is being displayed on the application layer.&lt;br /&gt;&lt;br /&gt;Presentation Layer:&lt;br /&gt;&lt;br /&gt;private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)&lt;br /&gt;{&lt;br /&gt;// Object of the Property layer&lt;br /&gt;       clsStudent objproperty=new clsStudent();&lt;br /&gt; &lt;br /&gt;// Object of the business layer&lt;br /&gt;       clsStudentInfo objbs=new clsStudentInfo();           &lt;br /&gt; &lt;br /&gt;// Object of the dataset in which we receive the data sent by the business layer&lt;br /&gt;       DataSet ds=new DataSet();                                                &lt;br /&gt; &lt;br /&gt;// here we are placing the value in the property â€œIDâ€ using the object of the  &lt;br /&gt;   property layer&lt;br /&gt;       objproperty.id=int.Parse(DataGrid1.SelectedItem.Cells[1].Text.ToString());&lt;br /&gt; &lt;br /&gt;// In ths following code we are calling a function from the business layer and    passing the object of the property layer which will carry the ID till the &lt;br /&gt;   database.&lt;br /&gt;              ds=objbs.GetAllStudentBsIDWise(objproperty);&lt;br /&gt; &lt;br /&gt;// What ever the data has been returned by the above function into the dataset is&lt;br /&gt;   being populate through the presentation laye.&lt;br /&gt;              txtId.Text=ds.Tables[0].Rows[0][0].ToString();&lt;br /&gt;             txtFname.Text=ds.Tables[0].Rows[0][1].ToString();&lt;br /&gt;             txtAddress.Text=ds.Tables[0].Rows[0][2].ToString();&lt;br /&gt;             txtemail.Text=ds.Tables[0].Rows[0][3].ToString();&lt;br /&gt;             Image1.ImageUrl=ds.Tables[0].Rows[0][4].ToString();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Property Layer&lt;br /&gt;&lt;br /&gt;// These are the properties has been defined in the property layer. Using the object of the property layer we can set or get the data to or from these properties.&lt;br /&gt;public class clsStudent    // Class for Student Table&lt;br /&gt;       {&lt;br /&gt;             private int _id;&lt;br /&gt;             private string _Name;&lt;br /&gt;             private string _Address;&lt;br /&gt;             private string _Email;&lt;br /&gt;             private string _Picture;&lt;br /&gt;            &lt;br /&gt;             public int id // Property to set or get the value into _id variable&lt;br /&gt;             {&lt;br /&gt;                    get{return _id;}&lt;br /&gt;                    set{_id=value;}&lt;br /&gt;             }&lt;br /&gt;      &lt;br /&gt;             public string Name&lt;br /&gt;             {&lt;br /&gt;                    get{return _Name;}&lt;br /&gt;                    set{_Name=value;}&lt;br /&gt;             }&lt;br /&gt; &lt;br /&gt;             public string Address&lt;br /&gt;             {&lt;br /&gt;                    get{return _Address;}&lt;br /&gt;                    set{_Address=value;}&lt;br /&gt;             }&lt;br /&gt;            &lt;br /&gt;             public string Email&lt;br /&gt;             {&lt;br /&gt;                    get{return _Email;}&lt;br /&gt;                    set{_Email=value;}&lt;br /&gt;             }&lt;br /&gt; &lt;br /&gt;             public string Picture&lt;br /&gt;             {&lt;br /&gt;                    get{return _Picture;}&lt;br /&gt;                    set{ _Picture=value;}&lt;br /&gt;             }&lt;br /&gt;       }&lt;br /&gt;&lt;br /&gt;Business Layer:&lt;br /&gt;&lt;br /&gt;"Obj" is the object of the clsStudent class has been defined in the property layer. This function is receiving the property object and passing it to the datalayer class&lt;br /&gt;&lt;br /&gt;// this is the function of the business layer which accepts the data from the application layer and passes it to the data layer.&lt;br /&gt; &lt;br /&gt;public class clsStudentInfo&lt;br /&gt;{&lt;br /&gt;public DataSet GetAllStudentBsIDWise(clsStudent obj)&lt;br /&gt;{&lt;br /&gt;       DataSet ds=new DataSet();&lt;br /&gt;ds=objdt.getdata_dtIDWise(obj);// Calling of Data layer function&lt;br /&gt;       return ds;                        &lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Datalayer Layer&lt;br /&gt;&lt;br /&gt;// this is the datalayer function which is receiving the data from the business layer and&lt;br /&gt;   performing the required operation into the database&lt;br /&gt; &lt;br /&gt;public class clsStudentData // Data layer class&lt;br /&gt;{&lt;br /&gt;   public DataSet getdata_dtIDWise(clsStudent obj) // object of property layer class&lt;br /&gt;       {&lt;br /&gt;       DataSet ds;&lt;br /&gt;       string sql;&lt;br /&gt;       sql="select * from student where StudentId="+obj.id+" order by StudentId";&lt;br /&gt;       ds=new DataSet();&lt;br /&gt;// this is the datalayer function which accepts trhe sql query and performs the&lt;br /&gt;   corresponding operation&lt;br /&gt;       ds=objdt.ExecuteSql(sql);&lt;br /&gt;       return ds;&lt;br /&gt;       }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;This is the implementation of the single function for more please see the sample application attached with the note.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2770957963577646170-518473059467668402?l=ravi-testingblog.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ravi-testingblog.blogspot.com/feeds/518473059467668402/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ravi-testingblog.blogspot.com/2009/07/three-tier-architecture-in-aspnet.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2770957963577646170/posts/default/518473059467668402'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2770957963577646170/posts/default/518473059467668402'/><link rel='alternate' type='text/html' href='http://ravi-testingblog.blogspot.com/2009/07/three-tier-architecture-in-aspnet.html' title='Three Tier Architecture in ASP.NET'/><author><name>ravi</name><uri>http://www.blogger.com/profile/07517056757956161890</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2770957963577646170.post-3672359788576394999</id><published>2009-07-28T08:39:00.000-07:00</published><updated>2009-07-28T09:49:40.389-07:00</updated><title type='text'>Generating Excel Report in ASP.NET 2.0</title><content type='html'>&lt;h2&gt;Introduction&lt;/h2&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://revtwt.com/index.php?id=31467"&gt;&lt;img style="margin: 0pt 0pt 10px 10px; float: right; cursor: pointer;" src="http://revtwt.com/images/TwtAd_referral02.jpg" alt="" id="BLOGGER_PHOTO_ID_5363553245623090306" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;p class="ArticleText"&gt;Excel is such a powerful tool that almost all companies make extensive use of it for analyzing data. Moreover, the internet is not as it was 10 years ago. Almost all pages that are out there on the internet are dynamic ones, that is, interacts with a database in backend to produce results. Sometimes, data that are displayed, if made available on in an Excel file, proper analysis of the data often helps in making more accurate decisions by using Excel features. In this tutorial, we will learn how to interact with Excel files, both reading and writing. You can download &lt;a href="http://www.beansoftware.com/ASP.NET-Tutorials/Examples/Excel-Report.zip"&gt; Excel Reports Example ASP.NET Project&lt;/a&gt; associated with this tutorial.&lt;/p&gt;&lt;h2&gt;Excel - ASP.NET Scenarios&lt;/h2&gt;&lt;div&gt;&lt;a href="http://revtwt.com/index.php?id=31467"&gt;&lt;img src="http://revtwt.com/images/TwtAd_referral01.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;p class="ArticleText"&gt;For the purpose of this tutorial, an Excel sheet had been prepared that holds record of students  and their respective marks in subjects.  Please note that these records are fictitious and had been input randomly.&lt;/p&gt; &lt;div align="center"&gt;&lt;table style="width: 167.65pt; border-collapse: collapse;" id="table1" border="0" cellpadding="0" cellspacing="0" width="224"&gt;  &lt;tbody&gt;&lt;tr style="height: 12.75pt;"&gt;   &lt;td style="padding: 0cm 5.4pt; width: 56.95pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="76"&gt;   &lt;p class="ArticleText" style="text-align: center;" align="center"&gt;&lt;b&gt;   &lt;span&gt;StudentId&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;/td&gt;   &lt;td style="padding: 0cm 5.4pt; width: 110.7pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="148"&gt;   &lt;p class="ArticleText"&gt;&lt;b&gt;   &lt;span style=";font-family:Arial;font-size:10pt;"  &gt;StudentName&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;/td&gt;  &lt;/tr&gt;  &lt;tr style="height: 12.75pt;"&gt;   &lt;td style="padding: 0cm 5.4pt; width: 56.95pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="76"&gt;   &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;   &lt;span&gt;10001&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;   &lt;td style="padding: 0cm 5.4pt; width: 110.7pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="148"&gt;   &lt;p class="ArticleText"&gt;   &lt;span&gt;John    Thomas&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;  &lt;/tr&gt;  &lt;tr style="height: 12.75pt;"&gt;   &lt;td style="padding: 0cm 5.4pt; width: 56.95pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="76"&gt;   &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;   &lt;span&gt;10002&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;   &lt;td style="padding: 0cm 5.4pt; width: 110.7pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="148"&gt;   &lt;p class="ArticleText"&gt;   &lt;span&gt;Terry Lane&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;  &lt;/tr&gt;  &lt;tr style="height: 12.75pt;"&gt;   &lt;td style="padding: 0cm 5.4pt; width: 56.95pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="76"&gt;   &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;   &lt;span&gt;10003&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;   &lt;td style="padding: 0cm 5.4pt; width: 110.7pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="148"&gt;   &lt;p class="ArticleText"&gt;   &lt;span&gt;Anne Marie&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;  &lt;/tr&gt;  &lt;tr style="height: 12.75pt;"&gt;   &lt;td style="padding: 0cm 5.4pt; width: 56.95pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="76"&gt;   &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;   &lt;span&gt;10004&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;   &lt;td style="padding: 0cm 5.4pt; width: 110.7pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="148"&gt;   &lt;p class="ArticleText"&gt;   &lt;span&gt;Tom Sawyer&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;  &lt;/tr&gt;  &lt;tr style="height: 12.75pt;"&gt;   &lt;td style="padding: 0cm 5.4pt; width: 56.95pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="76"&gt;   &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;   &lt;span&gt;10005&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;   &lt;td style="padding: 0cm 5.4pt; width: 110.7pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="148"&gt;   &lt;p class="ArticleText"&gt;   &lt;span&gt;Derek Ince&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;  &lt;/tr&gt;  &lt;tr style="height: 12.75pt;"&gt;   &lt;td style="padding: 0cm 5.4pt; width: 56.95pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="76"&gt;   &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;   &lt;span&gt;10006&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;   &lt;td style="padding: 0cm 5.4pt; width: 110.7pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="148"&gt;   &lt;p class="ArticleText"&gt;   &lt;span&gt;Emerson    Boyce&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;  &lt;/tr&gt;  &lt;tr style="height: 12.75pt;"&gt;   &lt;td style="padding: 0cm 5.4pt; width: 56.95pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="76"&gt;   &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;   &lt;span&gt;10007&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;   &lt;td style="padding: 0cm 5.4pt; width: 110.7pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="148"&gt;   &lt;p class="ArticleText"&gt;   &lt;span&gt;Thommas    Kolka&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;  &lt;/tr&gt;  &lt;tr style="height: 12.75pt;"&gt;   &lt;td style="padding: 0cm 5.4pt; width: 56.95pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="76"&gt;   &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;   &lt;span&gt;10008&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;   &lt;td style="padding: 0cm 5.4pt; width: 110.7pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="148"&gt;   &lt;p class="ArticleText"&gt;   &lt;span&gt;Edison    Hall&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;  &lt;/tr&gt;  &lt;tr style="height: 12.75pt;"&gt;   &lt;td style="padding: 0cm 5.4pt; width: 56.95pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="76"&gt;   &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;   &lt;span&gt;10009&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;   &lt;td style="padding: 0cm 5.4pt; width: 110.7pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="148"&gt;   &lt;p class="ArticleText"&gt;   &lt;span&gt;Teddy    Harewood&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;  &lt;/tr&gt;  &lt;tr style="height: 12.75pt;"&gt;   &lt;td style="padding: 0cm 5.4pt; width: 56.95pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="76"&gt;   &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;   &lt;span&gt;10010&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;   &lt;td style="padding: 0cm 5.4pt; width: 110.7pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="148"&gt;   &lt;p class="ArticleText"&gt;   &lt;span&gt;Williams    Yorkshire&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;  &lt;/tr&gt; &lt;/tbody&gt;&lt;/table&gt; &lt;p class="ArticleText"&gt;Fig 1.  Table holding information for students&lt;/p&gt;  &lt;/div&gt;  &lt;div align="center"&gt;&lt;table style="width: 104.95pt; border-collapse: collapse;" id="table2" border="0" cellpadding="0" cellspacing="0" width="140"&gt;   &lt;tbody&gt;&lt;tr style="height: 12.75pt;"&gt;    &lt;td style="padding: 0cm 5.4pt; width: 56.95pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="76"&gt;    &lt;p class="ArticleText"&gt;&lt;b&gt;    &lt;span style=";font-family:Arial;font-size:10pt;"  &gt;StudentId&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;/td&gt;    &lt;td style="padding: 0cm 5.4pt; width: 48pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="64"&gt;    &lt;p class="ArticleText"&gt;&lt;b&gt;    &lt;span style=";font-family:Arial;font-size:10pt;"  &gt;Marks&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;/td&gt;   &lt;/tr&gt;   &lt;tr style="height: 12.75pt;"&gt;    &lt;td style="padding: 0cm 5.4pt; width: 56.95pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="76"&gt;    &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;    &lt;span&gt;10001&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;    &lt;td style="padding: 0cm 5.4pt; width: 48pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="64"&gt;    &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;    &lt;span&gt;50&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;   &lt;/tr&gt;   &lt;tr style="height: 12.75pt;"&gt;    &lt;td style="padding: 0cm 5.4pt; width: 56.95pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="76"&gt;    &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;    &lt;span&gt;10002&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;    &lt;td style="padding: 0cm 5.4pt; width: 48pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="64"&gt;    &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;    &lt;span&gt;60&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;   &lt;/tr&gt;   &lt;tr style="height: 12.75pt;"&gt;    &lt;td style="padding: 0cm 5.4pt; width: 56.95pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="76"&gt;    &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;    &lt;span&gt;10003&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;    &lt;td style="padding: 0cm 5.4pt; width: 48pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="64"&gt;    &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;    &lt;span&gt;75&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;   &lt;/tr&gt;   &lt;tr style="height: 12.75pt;"&gt;    &lt;td style="padding: 0cm 5.4pt; width: 56.95pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="76"&gt;    &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;    &lt;span&gt;10004&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;    &lt;td style="padding: 0cm 5.4pt; width: 48pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="64"&gt;    &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;    &lt;span&gt;80&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;   &lt;/tr&gt;   &lt;tr style="height: 12.75pt;"&gt;    &lt;td style="padding: 0cm 5.4pt; width: 56.95pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="76"&gt;    &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;    &lt;span&gt;10005&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;    &lt;td style="padding: 0cm 5.4pt; width: 48pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="64"&gt;    &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;    &lt;span&gt;90&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;   &lt;/tr&gt;   &lt;tr style="height: 12.75pt;"&gt;    &lt;td style="padding: 0cm 5.4pt; width: 56.95pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="76"&gt;    &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;    &lt;span&gt;10006&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;    &lt;td style="padding: 0cm 5.4pt; width: 48pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="64"&gt;    &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;    &lt;span&gt;95&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;   &lt;/tr&gt;   &lt;tr style="height: 12.75pt;"&gt;    &lt;td style="padding: 0cm 5.4pt; width: 56.95pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="76"&gt;    &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;    &lt;span&gt;10007&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;    &lt;td style="padding: 0cm 5.4pt; width: 48pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="64"&gt;    &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;    &lt;span&gt;100&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;   &lt;/tr&gt;   &lt;tr style="height: 12.75pt;"&gt;    &lt;td style="padding: 0cm 5.4pt; width: 56.95pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="76"&gt;    &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;    &lt;span&gt;10008&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;    &lt;td style="padding: 0cm 5.4pt; width: 48pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="64"&gt;    &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;    &lt;span&gt;20&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;   &lt;/tr&gt;   &lt;tr style="height: 12.75pt;"&gt;    &lt;td style="padding: 0cm 5.4pt; width: 56.95pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="76"&gt;    &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;    &lt;span&gt;10009&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;    &lt;td style="padding: 0cm 5.4pt; width: 48pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="64"&gt;    &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;    &lt;span&gt;48&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;   &lt;/tr&gt;   &lt;tr style="height: 12.75pt;"&gt;    &lt;td style="padding: 0cm 5.4pt; width: 56.95pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="76"&gt;    &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;    &lt;span&gt;10010&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;    &lt;td style="padding: 0cm 5.4pt; width: 48pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="64"&gt;    &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;    &lt;span&gt;66&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;   &lt;/tr&gt;  &lt;/tbody&gt;&lt;/table&gt;  &lt;p class="ArticleText"&gt;Fig 2.  Table holding student marks in Mathematics subject&lt;/p&gt;&lt;/div&gt;   &lt;div align="center"&gt;&lt;table style="width: 110.5pt; border-collapse: collapse;" id="table3" border="0" cellpadding="0" cellspacing="0" width="147"&gt;  &lt;tbody&gt;&lt;tr style="height: 12.75pt;"&gt;   &lt;td style="padding: 0cm 5.4pt; width: 62.5pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="83"&gt;   &lt;p class="ArticleText" style="text-align: center;" align="center"&gt;&lt;b&gt;   &lt;span&gt;StudentId&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;/td&gt;   &lt;td style="padding: 0cm 5.4pt; width: 48pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="64"&gt;   &lt;p class="ArticleText" style="text-align: center;" align="center"&gt;&lt;b&gt;   &lt;span&gt;Marks&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;/td&gt;  &lt;/tr&gt;  &lt;tr style="height: 12.75pt;"&gt;   &lt;td style="padding: 0cm 5.4pt; width: 62.5pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="83"&gt;   &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;   &lt;span&gt;10001&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;   &lt;td style="padding: 0cm 5.4pt; width: 48pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="64"&gt;   &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;   &lt;span&gt;30&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;  &lt;/tr&gt;  &lt;tr style="height: 12.75pt;"&gt;   &lt;td style="padding: 0cm 5.4pt; width: 62.5pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="83"&gt;   &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;   &lt;span&gt;10002&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;   &lt;td style="padding: 0cm 5.4pt; width: 48pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="64"&gt;   &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;   &lt;span&gt;25&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;  &lt;/tr&gt;  &lt;tr style="height: 12.75pt;"&gt;   &lt;td style="padding: 0cm 5.4pt; width: 62.5pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="83"&gt;   &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;   &lt;span&gt;10003&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;   &lt;td style="padding: 0cm 5.4pt; width: 48pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="64"&gt;   &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;   &lt;span&gt;65&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;  &lt;/tr&gt;  &lt;tr style="height: 12.75pt;"&gt;   &lt;td style="padding: 0cm 5.4pt; width: 62.5pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="83"&gt;   &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;   &lt;span&gt;10004&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;   &lt;td style="padding: 0cm 5.4pt; width: 48pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="64"&gt;   &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;   &lt;span&gt;78&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;  &lt;/tr&gt;  &lt;tr style="height: 12.75pt;"&gt;   &lt;td style="padding: 0cm 5.4pt; width: 62.5pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="83"&gt;   &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;   &lt;span&gt;10005&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;   &lt;td style="padding: 0cm 5.4pt; width: 48pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="64"&gt;   &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;   &lt;span&gt;95&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;  &lt;/tr&gt;  &lt;tr style="height: 12.75pt;"&gt;   &lt;td style="padding: 0cm 5.4pt; width: 62.5pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="83"&gt;   &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;   &lt;span&gt;10006&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;   &lt;td style="padding: 0cm 5.4pt; width: 48pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="64"&gt;   &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;   &lt;span&gt;64&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;  &lt;/tr&gt;  &lt;tr style="height: 12.75pt;"&gt;   &lt;td style="padding: 0cm 5.4pt; width: 62.5pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="83"&gt;   &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;   &lt;span&gt;10007&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;   &lt;td style="padding: 0cm 5.4pt; width: 48pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="64"&gt;   &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;   &lt;span&gt;32&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;  &lt;/tr&gt;  &lt;tr style="height: 12.75pt;"&gt;   &lt;td style="padding: 0cm 5.4pt; width: 62.5pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="83"&gt;   &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;   &lt;span&gt;10008&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;   &lt;td style="padding: 0cm 5.4pt; width: 48pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="64"&gt;   &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;   &lt;span&gt;88&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;  &lt;/tr&gt;  &lt;tr style="height: 12.75pt;"&gt;   &lt;td style="padding: 0cm 5.4pt; width: 62.5pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="83"&gt;   &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;   &lt;span&gt;10009&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;   &lt;td style="padding: 0cm 5.4pt; width: 48pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="64"&gt;   &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;   &lt;span&gt;95&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;  &lt;/tr&gt;  &lt;tr style="height: 12.75pt;"&gt;   &lt;td style="padding: 0cm 5.4pt; width: 62.5pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="83"&gt;   &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;   &lt;span&gt;10010&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;   &lt;td style="padding: 0cm 5.4pt; width: 48pt; height: 12.75pt;" nowrap="nowrap" valign="bottom" width="64"&gt;   &lt;p class="ArticleText" style="text-align: right;" align="right"&gt;   &lt;span&gt;99&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;  &lt;/tr&gt; &lt;/tbody&gt;&lt;/table&gt; &lt;p class="ArticleText"&gt;Fig 3. Table holding student marks in Geography subject&lt;/p&gt; &lt;/div&gt; &lt;h2&gt;Interacting with Excel&lt;/h2&gt; &lt;p class="ArticleText"&gt;Fortunately, there are COM objects that are available to interact with the Excel file. Also, we can make use of the OleDB class to treat the Excel file as a simple table. In this way, we can fire SQL queries to the Excel file so that we can retrieve data from the sheets. An analogy can be made to a database. The Excel Workbook is considered as the Database while the Sheets are considered as tables. Microsoft Excel 11.0 Object Library should be added as Reference to get the features for interacting with Excel.&lt;/p&gt; &lt;div align="center"&gt;&lt;p class="ArticleText"&gt;&lt;img src="http://www.beansoftware.com/ASP.NET-Tutorials/Images/Excel-Reporting-Add-Reference.jpg" /&gt;&lt;br /&gt;Fig 4. Adding Microsoft Excel 11.0 Object Library as Reference&lt;/p&gt;&lt;/div&gt; &lt;p class="ArticleText"&gt;The Excel file is then added to the DataSource folder in ASAP.NET to be interacted with.   To connect to a certain database, a connection string is needed.  The same thing applies for Excel.   The following is a key added in the Web.Config file for future reference.&lt;/p&gt;&lt;p class="ArticleText"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;add&lt;/span&gt;&lt;span style="color:blue;"&gt; &lt;/span&gt; &lt;span style="color:red;"&gt; name&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;&lt;span&gt;"&lt;span style="color:blue;"&gt;ExcelConnection&lt;/span&gt;"&lt;span style="color:blue;"&gt; &lt;/span&gt;&lt;span style="color:red;"&gt;connectionString&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;Provider=Microsoft.Jet.Oledb.4.0;Data  Source=|DataDirectory|\SchoolMgt.xls;Extended Properties=Excel 8.0&lt;/span&gt;"&lt;span style="color:blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="ArticleText"&gt;Note that our Excel file which contains the necessary data will be SchoolMgt.xls,  as written in the connection string.&lt;/p&gt; &lt;div align="center"&gt;&lt;p class="ArticleText"&gt;&lt;img src="http://www.beansoftware.com/ASP.NET-Tutorials/Images/Excel-Reporting-Example.jpg" /&gt;&lt;br /&gt;Fig 5. Excel file illustrating Excel Book containing data sheets with data&lt;/p&gt;&lt;/div&gt; &lt;h2&gt;Reading data from Excel sheets&lt;/h2&gt; &lt;p class="ArticleText"&gt;&lt;br /&gt;Let us build an interface for getting data from the Excel file.&lt;/p&gt; &lt;div align="center"&gt;&lt;p class="ArticleText"&gt;&lt;img src="http://www.beansoftware.com/ASP.NET-Tutorials/Images/Excel-Reporting-Interface.jpg" /&gt;&lt;br /&gt;Fig 6. Interface to search records per table selected.&lt;/p&gt;&lt;/div&gt; &lt;p class="ArticleText"&gt;It is important to import the following:&lt;/p&gt; &lt;p class="ArticleText"&gt;&lt;span style="color:blue;"&gt; Imports&lt;/span&gt;&lt;span&gt;  System.Data.OleDb&lt;/span&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt; Imports&lt;/span&gt;  System.Data&lt;/p&gt; &lt;p class="ArticleText"&gt;The following code creates the connection and builds the query based on the selected table.&lt;/p&gt; &lt;p class="ArticleText"&gt;&lt;span style="color:blue;"&gt; Protected&lt;/span&gt;&lt;span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; btnSearch_Click(&lt;span style="color:blue;"&gt;ByVal&lt;/span&gt;  sender &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Object&lt;/span&gt;, &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; e &lt;span style="color:blue;"&gt;As&lt;/span&gt;  System.EventArgs) &lt;span style="color:blue;"&gt;Handles&lt;/span&gt; btnSearch.Click&lt;/span&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt; Try&lt;/span&gt;&lt;br /&gt;&lt;span&gt;             &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; strExcelConn &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;String&lt;/span&gt; =  System.Configuration.ConfigurationManager.ConnectionStrings.Item(&lt;span style="color:maroon;"&gt;"ExcelConnection"&lt;/span&gt;).ToString()&lt;/span&gt;&lt;br /&gt;&lt;span&gt;             &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; dbConn &lt;span style="color:blue;"&gt; As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt; OleDbConnection(strExcelConn)&lt;/span&gt;&lt;br /&gt;&lt;span&gt;             &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; strSQL &lt;span style="color:blue;"&gt; As&lt;/span&gt; &lt;span style="color:blue;"&gt;String&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;span&gt;             strSQL = &lt;span style="color:maroon;"&gt;"SELECT * FROM ["&lt;/span&gt; &amp;amp;  cmbSheets.SelectedItem.ToString() &amp;amp; &lt;span style="color:maroon;"&gt;"$]"&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:maroon;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;span&gt;             dbConn.Open()&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span&gt;             &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; cmd &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt; OleDbCommand(strSQL, dbConn)&lt;/span&gt;&lt;br /&gt;&lt;span&gt;             &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; dsExcel &lt;span style="color:blue;"&gt; As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt; DataSet&lt;/span&gt;&lt;br /&gt;&lt;span&gt;             &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; daExcel &lt;span style="color:blue;"&gt; As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt; OleDbDataAdapter(cmd)&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span&gt;             daExcel.Fill(dsExcel)&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span&gt;             dgResults.DataSource = dsExcel&lt;/span&gt;&lt;br /&gt;&lt;span&gt;             dgResults.DataBind()&lt;/span&gt;&lt;br /&gt;&lt;span&gt;        &lt;span style="color:blue;"&gt;Catch&lt;/span&gt; ex &lt;span style="color:blue;"&gt;As&lt;/span&gt;  Exception&lt;/span&gt;&lt;br /&gt;&lt;span&gt;             &lt;span style="color:blue;"&gt;Throw&lt;/span&gt; ex&lt;/span&gt;&lt;br /&gt;&lt;span&gt;        &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Try&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt; End&lt;/span&gt;&lt;span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="ArticleText"&gt;Please note that the table name in the SELECT query should be in the format [TableName$],  which the square brackets and the dollar sign at the end.&lt;/p&gt; &lt;h2&gt;Playing with SELECT statements&lt;/h2&gt; &lt;p class="ArticleText"&gt;As you have guessed, the Excel file has turned out to be a normal database.   The transparency that the OleDB connection had made is exceptional.   Also, note that the first row is taken to contain field names for the columns.&lt;/p&gt; &lt;p class="ArticleText"&gt;Now, let us get the marks of the students in Mathematics, Geography and Total,  sorted in Descending order.&lt;/p&gt; &lt;p class="ArticleText"&gt;&lt;span style="color:blue;"&gt; Protected&lt;/span&gt;&lt;span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; btnGenerateReport_Click(&lt;span style="color:blue;"&gt;ByVal&lt;/span&gt;  sender &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Object&lt;/span&gt;, &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; e &lt;span style="color:blue;"&gt;As&lt;/span&gt;  System.EventArgs) &lt;span style="color:blue;"&gt;Handles&lt;/span&gt;  btnGenerateReport.Click&lt;/span&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt; Try&lt;/span&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt; Dim&lt;/span&gt;&lt;span&gt;  strExcelConn &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;String&lt;/span&gt;  = System.Configuration.ConfigurationManager.ConnectionStrings.Item(&lt;span style="color:maroon;"&gt;"ExcelConnection"&lt;/span&gt;).ToString()&lt;/span&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt; Dim&lt;/span&gt;&lt;span&gt;  dbConn &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt;  OleDbConnection(strExcelConn)&lt;/span&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt; Dim&lt;/span&gt;&lt;span&gt;  strSQL &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;String&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;span&gt;strSQL = &lt;span style="color:maroon;"&gt;"SELECT S.StudentId, S.StudentName, M.Marks, G.Marks,  (M.Marks+G.Marks) AS Total "&lt;/span&gt;&amp;amp; _&lt;/span&gt;&lt;br /&gt;&lt;span&gt;                      &lt;span style="color:maroon;"&gt;"FROM [Students$] S,  [Mathematics$] M, [Geography$] G "&lt;/span&gt;&amp;amp; _&lt;/span&gt;&lt;br /&gt;&lt;span&gt;                      &lt;span style="color:maroon;"&gt;"WHERE(S.StudentId = M.StudentId  And S.StudentId = G.StudentId) "&lt;/span&gt; &amp;amp; _&lt;/span&gt;&lt;br /&gt;&lt;span&gt;                      &lt;span style="color:maroon;"&gt;"ORDER BY (M.Marks+G.Marks) DESC"&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt; dbConn.Open()&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt; Dim&lt;/span&gt;&lt;span&gt;  cmd &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt;  OleDbCommand(strSQL, dbConn)&lt;/span&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt; Dim&lt;/span&gt;&lt;span&gt;  dsExcel &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt;  DataSet&lt;/span&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt; Dim&lt;/span&gt;&lt;span&gt;  daExcel &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt;  OleDbDataAdapter(cmd)&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span&gt; daExcel.Fill(dsExcel)&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span&gt; dgReports.DataSource = dsExcel&lt;/span&gt;&lt;br /&gt;&lt;span&gt; dgReports.DataBind()&lt;/span&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt; Catch&lt;/span&gt;&lt;span&gt;  ex &lt;span style="color:blue;"&gt;As&lt;/span&gt; Exception&lt;/span&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt; Throw&lt;/span&gt;&lt;span&gt;  ex&lt;/span&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt; End&lt;/span&gt;&lt;span&gt; &lt;span style="color:blue;"&gt;Try&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt; End&lt;/span&gt;&lt;span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;div align="center"&gt;&lt;p class="ArticleText"&gt;&lt;img src="http://www.beansoftware.com/ASP.NET-Tutorials/Images/Excel-Reporting-Generate.jpg" /&gt;&lt;br /&gt;Fig 7. Report generating the students' marks and total, sorted in descending order.&lt;/p&gt;&lt;/div&gt; &lt;h2&gt;Generating Excel Reports&lt;/h2&gt; &lt;p class="ArticleText"&gt;Two ways for generating Excel report will be discussed in this tutorial.   The first one is using the "Response" class and the second one by manipulating the Excel objects that  "Microsoft Excel 11.0" Objects provide.&lt;/p&gt;  &lt;p class="ArticleText"&gt;The following piece of code illustrates how this is done&lt;/p&gt;  &lt;p class="ArticleText"&gt; &lt;span&gt;    &lt;span style="color:blue;"&gt;Protected&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt;  btnToExcelByResponse_Click(&lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; sender &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Object&lt;/span&gt;, &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; e &lt;span style="color:blue;"&gt;As&lt;/span&gt;  System.EventArgs) &lt;span style="color:blue;"&gt;Handles&lt;/span&gt;  btnToExcelByResponse.Click&lt;/span&gt;&lt;br /&gt;&lt;span&gt;        &lt;span style="color:green;"&gt;'   Variables declaration&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;        &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; dsExport &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt; DataSet()&lt;/span&gt;&lt;br /&gt;&lt;span&gt;        &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; tw &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt; System.IO.StringWriter()&lt;/span&gt;&lt;br /&gt;&lt;span&gt;        &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; hw &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt; System.Web.UI.HtmlTextWriter(tw)&lt;/span&gt;&lt;br /&gt;&lt;span&gt;        &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; dgGrid &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;New&lt;/span&gt; DataGrid()&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span&gt;         dgGrid.DataSource = getData()&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span&gt;        &lt;span style="color:green;"&gt;'   Report Header&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;         hw.WriteLine(&lt;span style="color:maroon;"&gt;"&amp;lt;b&amp;gt;&amp;lt;u&amp;gt;&amp;lt;font size='5'&amp;gt; Student Marking  Report &amp;lt;/font&amp;gt;&amp;lt;/u&amp;gt;&amp;lt;/b&amp;gt;"&lt;/span&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span&gt;        &lt;span style="color:green;"&gt;'   Get the HTML for the control.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;         dgGrid.HeaderStyle.Font.Bold = &lt;span style="color:blue;"&gt;True&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;         dgGrid.DataBind()&lt;/span&gt;&lt;br /&gt;&lt;span&gt;         dgGrid.RenderControl(hw)&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span&gt;        &lt;span style="color:green;"&gt;'   Write the HTML back to the browser.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;         Response.ContentType = &lt;span style="color:maroon;"&gt;"application/vnd.ms-excel"&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;        &lt;span style="color:blue;"&gt;Me&lt;/span&gt;.EnableViewState = &lt;span style="color:blue;"&gt; False&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;         Response.Write(tw.ToString())&lt;/span&gt;&lt;br /&gt;&lt;span&gt;         Response.End()&lt;/span&gt;&lt;br /&gt;&lt;span&gt;    &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;ul type="circle"&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;First, we create the dataset that will hold the records.   Then, a StringWriter object (tw) is also created along with an HtmlTextWriter (hw)  which takes as parameter the text writer object.  They are important for rendering purposes;  i.e. html tags can be applied like bold, italic etc. to the resulting Excel report.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;Also, a datagrid object which will store the dataset of records.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;Note that function getData() in the code snippet only  contains the codes in the previous example which returns a dataset of records.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;hw.WriteLine(&lt;span style="color: rgb(128, 0, 0);"&gt;"&amp;lt;b&amp;gt;&lt;u&gt;&lt;span style=""&gt; Student Marking Report &lt;/span&gt;&lt;/u&gt;"&lt;/span&gt;) shows that you can also combine HTML codes with the output for proper display.   In our example, the font size will be 5, underlined and bold.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;dgGrid.RenderControl(hw) renders the HTML object with the data that the datagrid contains.   The result is normally formatted in a tabular format.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;Response.ContentType =  &lt;span style="color: rgb(128, 0, 0);"&gt; "application/vnd.ms-excel"&lt;/span&gt; makes the Reponse object output to Excel.&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p class="ArticleText"&gt;Response.Write(tw.ToString()) outputs the formatted object to Excel.&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt; &lt;div align="center"&gt;&lt;p class="ArticleText"&gt;&lt;img src="http://www.beansoftware.com/ASP.NET-Tutorials/Images/Excel-Reporting-Output.jpg" /&gt;&lt;br /&gt;Figure 8. Output result showing the report that is generated in Excel&lt;/p&gt;&lt;/div&gt; &lt;p class="ArticleText"&gt;The second way for generating Excel reports from ASP.NET is to use the Microsoft Excel 11.0 Objects.   This method offers more fallibility in terms of manipulating the features that Excel provides and formatting the report.&lt;/p&gt; &lt;p class="ArticleText"&gt;The following code snippet makes use of the various Excel objects that Excel provides to generate the report.&lt;/p&gt; &lt;p class="ArticleText"&gt;&lt;span style="color:blue;"&gt; Imports&lt;/span&gt;&lt;span&gt;  System.Reflection&lt;/span&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt; Imports&lt;/span&gt;&lt;span&gt;  Excel = Microsoft.Office.Interop.Excel&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:10pt;"&gt; &lt;/span&gt;&lt;br /&gt;&lt;span&gt;    &lt;span style="color:blue;"&gt;Protected&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt;  btnToExcelByObjects_Click(&lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; sender &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Object&lt;/span&gt;, &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; e &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.EventArgs) &lt;span style="color:blue;"&gt; Handles&lt;/span&gt; btnToExcelByObjects.Click&lt;/span&gt;&lt;br /&gt;&lt;span&gt;        &lt;span style="color:blue;"&gt;Try&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;             &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; xlWorkBook &lt;span style="color:blue;"&gt;As&lt;/span&gt; Excel.Workbook&lt;/span&gt;&lt;br /&gt;&lt;span&gt;             &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; xlWorkSheet &lt;span style="color:blue;"&gt;As&lt;/span&gt; Excel.Worksheet&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span&gt;             xlWorkBook = &lt;span style="color:blue;"&gt;New&lt;/span&gt;  Excel.Application().Workbooks.Add(Missing.Value)&lt;/span&gt;&lt;br /&gt;&lt;span&gt;             xlWorkBook.Application.Visible = &lt;span style="color:blue;"&gt;True&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;             xlWorkSheet = xlWorkBook.ActiveSheet&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span&gt;             &lt;span style="color:green;"&gt;'   Gets the dataset containing the data&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;             &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; dsData &lt;span style="color:blue;"&gt; As&lt;/span&gt; DataSet = getData()&lt;/span&gt;&lt;br /&gt;&lt;span&gt;             &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; i &lt;span style="color:blue;"&gt;As&lt;/span&gt; &lt;span style="color:blue;"&gt;Integer&lt;/span&gt; = 2&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span&gt;             &lt;span style="color:green;"&gt;'   Outputting the fieldnames in pink bold  color&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;             xlWorkSheet.Cells(1, 1) = &lt;span style="color:maroon;"&gt;"Student ID"&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;             xlWorkSheet.Cells(1, 2) = &lt;span style="color:maroon;"&gt;"Student Name"&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;             xlWorkSheet.Cells(1, 3) = &lt;span style="color:maroon;"&gt;"Mathematics"&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;             xlWorkSheet.Cells(1, 4) = &lt;span style="color:maroon;"&gt;"Geography"&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;             xlWorkSheet.Cells(1, 5) = &lt;span style="color:maroon;"&gt;"Total"&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:maroon;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;span&gt;             xlWorkSheet.Range(&lt;span style="color:maroon;"&gt;"$A1:$E1"&lt;/span&gt;).Font.ColorIndex  = Excel.Constants.xlColor1&lt;/span&gt;&lt;br /&gt;&lt;span&gt;             xlWorkSheet.Range(&lt;span style="color:maroon;"&gt;"$A1:$E1"&lt;/span&gt;).Font.Bold  = &lt;span style="color:blue;"&gt;True&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;span&gt;             &lt;span style="color:green;"&gt;'   Outputting the data&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;             &lt;span style="color:blue;"&gt;For&lt;/span&gt; &lt;span style="color:blue;"&gt;Each&lt;/span&gt;  dr &lt;span style="color:blue;"&gt;As&lt;/span&gt; DataRow &lt;span style="color:blue;"&gt;In&lt;/span&gt;  dsData.Tables(0).Rows&lt;/span&gt;&lt;br /&gt;&lt;span&gt;                 xlWorkSheet.Cells(i, 1) = dr(0)&lt;/span&gt;&lt;br /&gt;&lt;span&gt;                 xlWorkSheet.Cells(i, 2) = dr(1)&lt;/span&gt;&lt;br /&gt;&lt;span&gt;                 xlWorkSheet.Cells(i, 3) = dr(2)&lt;/span&gt;&lt;br /&gt;&lt;span&gt;                 xlWorkSheet.Cells(i, 4) = dr(3)&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span&gt;                 &lt;span style="color:green;"&gt;'   Building the formula for  calculating the sum&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;                 xlWorkSheet.Cells(i, 5).Formula = &lt;span style="color:maroon;"&gt; "=SUM($C{0}:$D{0})"&lt;/span&gt;.Replace(&lt;span style="color:maroon;"&gt;"{0}"&lt;/span&gt;,  i.ToString())&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span&gt;                 &lt;span style="color:green;"&gt;'   Going to the next row&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;                 i = i + 1&lt;/span&gt;&lt;br /&gt;&lt;span&gt;             &lt;span style="color:blue;"&gt;Next&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;span&gt;             &lt;span style="color:green;"&gt;'   Auto fit the columns&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;             xlWorkSheet.Columns.AutoFit()&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span&gt;             &lt;span style="color:green;"&gt;'   Generating the graph&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;             &lt;span style="color:blue;"&gt;Dim&lt;/span&gt; chart &lt;span style="color:blue;"&gt; As&lt;/span&gt; Excel.Chart&lt;/span&gt;&lt;br /&gt;&lt;span&gt;             chart = xlWorkBook.Charts.Add()&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span&gt;             &lt;span style="color:blue;"&gt;With&lt;/span&gt; chart&lt;/span&gt;&lt;br /&gt;&lt;span&gt;                 .ChartType = Excel.XlChartType.xlColumnClustered&lt;/span&gt;&lt;br /&gt;&lt;span&gt;                 .SetSourceData(xlWorkSheet.Range(&lt;span style="color:maroon;"&gt;"A1:E11"&lt;/span&gt;),  2)&lt;/span&gt;&lt;br /&gt;&lt;span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span&gt;                 .HasTitle = &lt;span style="color:blue;"&gt;True&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;                 .ChartTitle.Characters.Text = &lt;span style="color:maroon;"&gt; "Students' marks"&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:maroon;"&gt;  &lt;/span&gt;&lt;br /&gt;&lt;span&gt;                 .Axes(1, Excel.XlAxisGroup.xlPrimary).HasTitle = &lt;span style="color:blue;"&gt;True&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;                 .Axes(1, Excel.XlAxisGroup.xlPrimary).AxisTitle.Characters.Text  = &lt;span style="color:maroon;"&gt;"Students"&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;                 .Axes(2, Excel.XlAxisGroup.xlPrimary).HasTitle = &lt;span style="color:blue;"&gt;True&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;                 .Axes(2, Excel.XlAxisGroup.xlPrimary).AxisTitle.Characters.Text  = &lt;span style="color:maroon;"&gt;"Marks"&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;             &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;With&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;        &lt;span style="color:blue;"&gt;Catch&lt;/span&gt; ex &lt;span style="color:blue;"&gt;As&lt;/span&gt;  Exception&lt;/span&gt;&lt;br /&gt;&lt;span&gt;             &lt;span style="color:blue;"&gt;Throw&lt;/span&gt; ex&lt;/span&gt;&lt;br /&gt;&lt;span&gt;        &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Try&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span&gt;    &lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="ArticleText"&gt;An Excel workbook is first created.  Then the active sheet is accessed by xlWorkSheet =  xlWorkBook.ActiveSheet.  After that, we get the data into the dataset, ready to be processed.   Using the Font property, the title is displayed in Pink bold colour.   Note how the Formula that Excel provides is properly used to calculate the SUM of the marks for  each student xlWorkSheet.Cells(i, 5).Formula = "=SUM($C{0}:$D{0})".Replace("{0}", i.ToString()). &lt;/p&gt; &lt;div align="center"&gt;&lt;p class="ArticleText"&gt;&lt;img src="http://www.beansoftware.com/ASP.NET-Tutorials/Images/Excel-Reporting-Output-Marked.jpg" /&gt;&lt;br /&gt;Figure 9. Excel Report having output the list of students and their respective marks.&lt;/p&gt;&lt;/div&gt; &lt;p class="ArticleText"&gt;A chart is also generated using the objects.   The x-axis consists of the names and id of each student.  The y-axis, the marks for the different students.   The chart is always handy for analysis of data.&lt;/p&gt; &lt;div align="center"&gt;&lt;p class="ArticleText"&gt;&lt;img src="http://www.beansoftware.com/ASP.NET-Tutorials/Images/Excel-Reporting-Chart.jpg" /&gt;&lt;br /&gt;Figure 10. Chart that is generated using Microsoft Excel 11.0 Objects&lt;/p&gt;&lt;/div&gt; &lt;h2&gt;Summary&lt;/h2&gt; &lt;p class="ArticleText"&gt;In this tutorial, you learned how to read data from Excel,  generate report using the Response objects and to play with the Microsoft Excel 11.0 objects provided.   Indeed, it is impossible to cover all features in this tutorial but many important techniques were covered  to help you down the line.  It is up to you to be creative and adapt the techniques to your scenarios.&lt;/p&gt; &lt;h2&gt;Going professional with NativeExcel for .NET&lt;/h2&gt; &lt;p&gt;Excel is powerful tool, but in real world development you rarely have Microsoft Office installed on your web server. There is no Excel on shared hosting and obtaining Office license for dedicated server is possible, but also pretty expensive. Fortunately, there is pure .NET component named &lt;a rel="nofollow" target="_top" href="http://www.beansoftware.com/Visit/NativeExcel/"&gt;Native Excel for .NET&lt;/a&gt; that creates or modifies Excel documents on the fly. For just $120 for single license you can use it on &lt;b&gt;unlimited&lt;/b&gt; number of web sites and servers (source code available too). It is easier to distribute application since you work with simple assembly written in C#, instead of COM Excel object. Best of all, you don't need to learn anything new because objects and properties are identical to Excel's, so if you understood this tutorial you are already expert for &lt;a rel="nofollow" target="_top" href="http://www.beansoftware.com/Visit/NativeExcel/"&gt;NativeExcel&lt;/a&gt; too :).&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2770957963577646170-3672359788576394999?l=ravi-testingblog.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ravi-testingblog.blogspot.com/feeds/3672359788576394999/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ravi-testingblog.blogspot.com/2009/07/generating-excel-report-in-aspnet-20.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2770957963577646170/posts/default/3672359788576394999'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2770957963577646170/posts/default/3672359788576394999'/><link rel='alternate' type='text/html' href='http://ravi-testingblog.blogspot.com/2009/07/generating-excel-report-in-aspnet-20.html' title='Generating Excel Report in ASP.NET 2.0'/><author><name>ravi</name><uri>http://www.blogger.com/profile/07517056757956161890</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2770957963577646170.post-1541359912237905601</id><published>2009-07-28T08:29:00.000-07:00</published><updated>2009-07-28T08:39:06.202-07:00</updated><title type='text'>Managing State in Web Services</title><content type='html'>Using session objects is always a good option for maintaining per user data. But the case of web services is different. In real life, Web services may be called thousands of times a day (for example, a web service maintained by Railway administration for seat reservation). In such cases, keeping per user data in the webserver would be a bad approach. If you have heard the term PARSSeM, there would be violations of different parameters then. We would need to adopt some other approach.&lt;br /&gt;&lt;br /&gt;&lt;p class="ArticleText"&gt;In this article, I would focus on how session data can be maintained by web services . Besides, I would describe using Application object in web services. Using Application state object is always an easy and less resource consuming option because it does not deal on per user basis. &lt;/p&gt;    &lt;h2&gt;How can you use Session State Object?&lt;/h2&gt; &lt;p class="ArticleText"&gt;In web applications, you would use session object like:&lt;/p&gt; &lt;p class="ArticleText"&gt;Session["Userid"] = input_id;&lt;/p&gt; &lt;p class="ArticleText"&gt;And for retrieving, &lt;/p&gt; &lt;p class="ArticleText"&gt;String user_id = (String) Session["Userid"] &lt;/p&gt; &lt;p class="ArticleText"&gt;Let say, we want to check how much time our business logic code in seat reservation web service took while fulfilling requests. The code for time spent calculation would be included in the same web service. &lt;/p&gt; &lt;p class="ArticleText"&gt;&lt;b&gt;Listing 1.1&lt;/b&gt; defines this service:&lt;/p&gt;&lt;p class="ArticleText"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p class="ArticleText"&gt;&lt;/p&gt;&lt;blockquote style="color: rgb(102, 102, 102);"&gt;1)    using System;&lt;br /&gt;    2)    using System.Web;&lt;br /&gt;    3)    using System.Web.Services;&lt;br /&gt;    4)    &lt;br /&gt;    5)    namespace WebBook&lt;br /&gt;    6)    {&lt;br /&gt;    7)       public class Reservation : System.Web.Services.WebService&lt;br /&gt;    8)       {&lt;br /&gt;    9)       &lt;br /&gt;    10)                       [ WebMethod (EnableSession = true) ]&lt;br /&gt;    11)                       public void ReserveSeat ()&lt;br /&gt;    12)                       {&lt;br /&gt;    13)                         DateTime init_value = System.DateTime.Now;&lt;br /&gt;    14)                         &lt;br /&gt;    15)                         // business logic for seat reservation&lt;br /&gt;    16)                         DateTime final_value = System.DateTime.Now;&lt;br /&gt;    17)                        &lt;br /&gt;    18)                         // using Application State object. Would explain it later&lt;br /&gt;    19)                         int total_requests = 0;&lt;br /&gt;    20)                         total_requests =  (int) Application["requests"];&lt;br /&gt;    21)                         total_requests = total_requests +1;&lt;br /&gt;    22)                         Application ["requests"] = total_requests;&lt;br /&gt;    23)                        &lt;br /&gt;    24)                         // Updating Total time spent in session&lt;br /&gt;    25)                         TimeSpan timespan1;&lt;br /&gt;    26)                         Object Session_time =  Session["Time"];&lt;br /&gt;    27)                        &lt;br /&gt;    28)                         if (Session_Time != null)&lt;br /&gt;    29)                         {&lt;br /&gt;    30)                            timespan1 = (TimeSpan) Session_time;&lt;br /&gt;    31)                         }&lt;br /&gt;    32)                         else&lt;br /&gt;    33)                         {&lt;br /&gt;    34)                            timespan1 = new TimeSpan (0,0,0,0,0);&lt;br /&gt;    35)                         }&lt;br /&gt;    36)                        &lt;br /&gt;    37)                         timespan1 += final_value - init_value;&lt;br /&gt;    38)                        &lt;br /&gt;    39)                         Session["Time"] = timespan1;&lt;br /&gt;    40)                       &lt;br /&gt;    41)                         [ WebMethod (EnableSession = true) ]&lt;br /&gt;    42)                         public String TotalTime()&lt;br /&gt;    43)                         {&lt;br /&gt;    44)                        &lt;br /&gt;    45)                          if (Session["Time"]==null)&lt;br /&gt;    46)                            return new System.TimeSpan().ToString();&lt;br /&gt;    47)                          else&lt;br /&gt;    48)                            return ((System.TimeSpan) Session["Time"]).ToString();&lt;br /&gt;    49)                         }&lt;br /&gt;    50)                       &lt;br /&gt;    51)                       }   &lt;/blockquote&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;p class="ArticleText"&gt;&lt;b&gt;Analysis of Listing 1.1&lt;/b&gt;&lt;/p&gt;   &lt;p class="ArticleText"&gt;The method ReserveSeat() do two things: Implements the business logic for reserving a seat and calculates the time spent while reserving seat(s). Before executing the business logic, it notes the current system data and time and at the end of execution, it again records the same. So, we can now calculate the time spent during execution of seat reservation code. Since we were supposed to calculate all the time spent till now by different users, we would take the previous Time value in the session variable and add it with newly calculated value. The other function TotalTime() returns the time spent so far. &lt;/p&gt; &lt;p class="ArticleText"&gt;&lt;b&gt;The problem: &lt;/b&gt;&lt;/p&gt;  &lt;p class="ArticleText"&gt;Web services can be called from web forms as well as console applications. But manipulating session object in the same way as we do in other web applications can create problems here.&lt;/p&gt;  &lt;p class="ArticleText"&gt; If you call the Seat Reservation web service from a web form, you would receive accurate results as long as cookies are enabled in your browser. If the cookies are disabled, you would not see correct results. I would explain shortly why it happens.&lt;/p&gt; &lt;p class="ArticleText"&gt;If you call this service from a console application, again the same problem of incorrect results would appear. You write a client console application like this:&lt;/p&gt; &lt;p class="ArticleText"&gt;&lt;b&gt;Listing 1.2&lt;/b&gt;&lt;/p&gt;&lt;p class="ArticleText"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p class="ArticleText"&gt;&lt;/p&gt;&lt;blockquote style="color: rgb(102, 102, 102);"&gt;1)    using System;&lt;br /&gt;2)    using System.Net;&lt;br /&gt;3)    using WebBook.localhost();&lt;br /&gt;4)    &lt;br /&gt;5)    namespace WebBook&lt;br /&gt;6)    {&lt;br /&gt;7)       public class ServiceClient&lt;br /&gt;8)       {&lt;br /&gt;9)              public static void Main()&lt;br /&gt;10)                       {&lt;br /&gt;11)                         // Using the Reservation class function&lt;br /&gt;12)                          Reservation res_obj = new Reservation();&lt;br /&gt;13)                          res_obj.ReserveSeat();&lt;br /&gt;14)                          res_obj.ReserveSeat();&lt;br /&gt;15)                       &lt;br /&gt;16)                          Console.WriteLine ("Total Time spent is:" +&lt;br /&gt;17)                          res_obj.TotalTime());&lt;br /&gt;18)                       }&lt;br /&gt;19)                       Console.ReadLine();&lt;br /&gt;20)           }&lt;br /&gt;21)     &lt;br /&gt;22)     }&lt;/blockquote&gt;&lt;p&gt;&lt;/p&gt;&lt;p class="ArticleText"&gt;If the total time spent for one time execution of function ReserveSeat() is 1 second, can you guess what should it display? Most probably, you would say: "Total Time spent is 00:00:02" but it would display "Total Time spent is 00:00:00". The problem is explained below:&lt;/p&gt; &lt;p class="ArticleText"&gt;To keep track of user sessions, ASP.NET uses browser cookies. When a browser accesses a site, a cookie and a session Object is created and session ID is stored in the cookie. Upon every subsequent access, this cookie is used to send Session ID to the webserver which contains userâ€™s session data (that is why, if a web service has thousands of clients, its webserver resources may be consumed quickly). The problem is, not all web browsers enable cookies. Also, console applications do not use session cookies. So using a session object is not possible in these cases.&lt;br /&gt;&lt;/p&gt;&lt;p class="ArticleText"&gt;&lt;b&gt;Solution: &lt;/b&gt;&lt;/p&gt; &lt;p class="ArticleText"&gt;There is a two step solution to this problem.&lt;/p&gt;  &lt;p class="ArticleText"&gt;1)    As I said, many clients would not use cookies. However for those that use cookies, disable them. This is achieved by making a simple change in web.config file:&lt;/p&gt;&lt;p class="ArticleText"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p class="ArticleText"&gt;&lt;/p&gt;&lt;blockquote style="color: rgb(102, 102, 102);"&gt;&amp;lt;configuration&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;system.web&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;compilation defaultLanguage= "C#" debug= "true" &amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;session cookieless = "true"&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/system.web&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/configuration&amp;gt;&lt;/blockquote&gt;&lt;p&gt;&lt;/p&gt;&lt;p class="ArticleText"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p class="ArticleText"&gt;2) We should force ASP.NET to seek some alternative solution for session handling. In the absence of cookies, ASP.NET uses a technique called "URL Munging". In this technique, session ID is placed in the url of the web service when the first method call is made. For example, in this case, we can have something like:&lt;/p&gt; &lt;p class="ArticleText"&gt;http://localhost/WebBook/(fdsjkalkjfal45kajs2)/Reservation.asmx&lt;/p&gt; &lt;p class="ArticleText"&gt;There is another issue: This new redirected URL will cause an exception in console application. Ignore the cause of this exception . Below is one way to recover from exception and generate the accurate results.&lt;/p&gt;&lt;p class="ArticleText"&gt;&lt;b&gt;Listing 1.3&lt;/b&gt;&lt;/p&gt;&lt;p class="ArticleText"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p class="ArticleText"&gt;&lt;/p&gt;&lt;blockquote style="color: rgb(102, 102, 102);"&gt;1)    using System;&lt;br /&gt;2)    using System.Net;&lt;br /&gt;3)    using WebBook.localhost();&lt;br /&gt;4)    &lt;br /&gt;5)    namespace WebBook&lt;br /&gt;6)    {&lt;br /&gt;7)       public class ServiceClient&lt;br /&gt;8)       {&lt;br /&gt;9)              public static void Main()&lt;br /&gt;10)                       {&lt;br /&gt;11)                          // Using the Reservation class function&lt;br /&gt;12)                          Reservation res_obj = new Reservation();&lt;br /&gt;13)                          // Now, We handle the exception that arises in console &lt;br /&gt;14)                          // application.&lt;br /&gt;15)                       &lt;br /&gt;16)                          try&lt;br /&gt;17)                          {&lt;br /&gt;18)                            res_obj.ReserveSeat();&lt;br /&gt;19)                          }&lt;br /&gt;20)                          catch (WebException e)&lt;br /&gt;21)                          {&lt;br /&gt;22)                          &lt;br /&gt;23)                          // This catch code would always execute because of the&lt;br /&gt;24)                          // exception. The exception message e would tell us the&lt;br /&gt;25)                          // munged redirected  url.&lt;br /&gt;26)                       &lt;br /&gt;27)                          String message = e.Message;&lt;br /&gt;28)                          &lt;br /&gt;29)                          // Since the message contain something like "Object moved to&lt;br /&gt;30)                          //  &amp;lt;a href ' " We would search this string in the error&lt;br /&gt;31)                          // message&lt;br /&gt;32)                       &lt;br /&gt;33)                          String to_search= "Object moved to &amp;lt;a href ' ";&lt;br /&gt;34)                          int desired_pos = message.IndexOf (to_search);&lt;br /&gt;35)                         &lt;br /&gt;36)                          // check if string exists in error message or there is some&lt;br /&gt;37)                          // other exception&lt;br /&gt;38)                       &lt;br /&gt;39)                          if (desired_pos &amp;gt; 0)&lt;br /&gt;40)                          {&lt;br /&gt;41)                         &lt;br /&gt;42)                          // Extract the message now. Message to be extracted from&lt;br /&gt;43)                          // start of message to 500 characters ahead, You can choose&lt;br /&gt;44)                          // to even more if message length is more than 500&lt;br /&gt;45)                       &lt;br /&gt;46)                          String extracted_message = message.SubString (desired_pos&lt;br /&gt;47)                          + 500)&lt;br /&gt;48)                         &lt;br /&gt;49)                          // Extract the url from the extracted message.&lt;br /&gt;50)                          // We would split the string using the ' character. The url&lt;br /&gt;51)                          // would be the 2nd element&lt;br /&gt;52)                         &lt;br /&gt;53)                          String[] broken_message = extracted_message.Split ('\'');&lt;br /&gt;54)                          String url= broken_message[1];&lt;br /&gt;55)                       &lt;br /&gt;56)                          // Now, lets redirect to access the right page and to see the&lt;br /&gt;57)                          // effect of session object.&lt;br /&gt;58)                          res_obj.Url = "http://localhost "+ url;&lt;br /&gt;59)                          }&lt;br /&gt;60)                       &lt;br /&gt;61)                       else&lt;br /&gt;62)                       {&lt;br /&gt;63)                       // Some other exception&lt;br /&gt;64)                       throw e;&lt;br /&gt;65)                       }&lt;br /&gt;66)                       &lt;br /&gt;67)                }&lt;br /&gt;68)                         // Now these calls would generate right results&lt;br /&gt;69)                          res_obj.ReserveSeat();&lt;br /&gt;70)                          res_obj.ReserveSeat();&lt;br /&gt;71)                       &lt;br /&gt;72)                          Console.WriteLine ("Total Time spent is:" +&lt;br /&gt;73)                          res_obj.TotalTime());&lt;br /&gt;74)           &lt;br /&gt;75)              }&lt;br /&gt;76)           Console.ReadLine();&lt;br /&gt;77)        }&lt;br /&gt;78)     }&lt;/blockquote&gt;&lt;p&gt;&lt;/p&gt;&lt;p class="ArticleText"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p class="ArticleText"&gt;When you make a client like Listing 1.3, you would get something like:&lt;/p&gt;  &lt;p class="ArticleText"&gt;Total Time spent is: 00:00:02&lt;/p&gt; &lt;h2&gt;Using Application State&lt;/h2&gt; &lt;p class="ArticleText"&gt;Please refer to Listing 1.1 and see Line 18-22. The application object can be used with the same ease in all web services as is used in other web applications. If you want to calculate the average time for processing of one seat reservation request, you can do so by dividing the output of TotalTime() function in Listing 1.1 to the "requests" variable in Application object which counts all the requests made.&lt;/p&gt;  &lt;h2&gt;Summary:&lt;/h2&gt;  &lt;ul class="ArticleText"&gt;&lt;li&gt;The use of Session object in web services is debatable.&lt;/li&gt;&lt;li&gt;ASP.NET use cookies to keep track of sessions.&lt;/li&gt;&lt;li&gt;Therefore, Session handling is not possible in console applications and web browsers having cookies disabled.&lt;/li&gt;&lt;li&gt;One possible solution is to disable cookies so that ASP.NET may use the technique "URL Munging". In this technique, it appends session ID to the url on first call.&lt;/li&gt;&lt;li&gt;Usage of Application object is easy and can be done with the same logic as web applications.&lt;/li&gt;&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2770957963577646170-1541359912237905601?l=ravi-testingblog.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ravi-testingblog.blogspot.com/feeds/1541359912237905601/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ravi-testingblog.blogspot.com/2009/07/managing-state-in-web-services.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2770957963577646170/posts/default/1541359912237905601'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2770957963577646170/posts/default/1541359912237905601'/><link rel='alternate' type='text/html' href='http://ravi-testingblog.blogspot.com/2009/07/managing-state-in-web-services.html' title='Managing State in Web Services'/><author><name>ravi</name><uri>http://www.blogger.com/profile/07517056757956161890</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2770957963577646170.post-4924946717158435167</id><published>2009-07-28T08:21:00.000-07:00</published><updated>2009-07-28T08:29:44.608-07:00</updated><title type='text'>Include Multiple .Config Files in ASP.NET Web Application</title><content type='html'>&lt;h2 style="color: rgb(102, 51, 51);"&gt;Introduction&lt;/h2&gt;&lt;span&gt;&lt;br /&gt;Microsoft ASP.Net provides a configuration system that can be used to keep our applications flexible at run-time. In this article, I will focus on using multiple configuration files. This is a rare technique and is unknown to many developers. The usage of multiple configuration file makes the application more secure and manageable.&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;h2 style="color: rgb(102, 51, 0);"&gt;Prerequisites&lt;/h2&gt; &lt;p class="ArticleText"&gt;&lt;span&gt;This tutorial assumes that you own a copy of Visual Studio 2005 or Visual Web Developer Express. It also assumes that you are familiar with ASP.Net 2.0 basics and have worked with web.config before.&lt;/span&gt;&lt;/p&gt;  &lt;h2 style="color: rgb(102, 51, 0);"&gt;Working with Web.Config&lt;/h2&gt; &lt;p class="ArticleText"&gt;&lt;span&gt;Web.config exposes an &amp;lt;appSettings&amp;gt; element that can be used as a place to store application settings like connection strings, file paths, etc. Using the web.config is an ideal method of creating a robust application that can quickly adapt to changes in its environment. For instance, if the connection string is stored in web.config and is being called from the web-pages from there, then changes in the connection string will have to be made in web.config only. Otherwise, the user would have to go to each page individually and update the connection string.&lt;/span&gt;&lt;/p&gt; &lt;p class="ArticleText"&gt;&lt;span&gt;Let us look at a basic web.config which holds our connection string.&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;blockquote&gt;&amp;lt;?xml version="1.0"?&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;configuration&amp;gt;&lt;br /&gt;&lt;br /&gt;    &amp;lt;appSettings/&amp;gt;&lt;br /&gt;&lt;br /&gt;    &amp;lt;connectionStrings/&amp;gt;&lt;br /&gt;&lt;br /&gt;    &amp;lt;system.web&amp;gt;&lt;br /&gt;&lt;br /&gt;        &amp;lt;compilation debug="false" strict="false" explicit="true" /&amp;gt;&lt;br /&gt;&lt;br /&gt;    &amp;lt;/system.web&amp;gt;&lt;br /&gt;&lt;br /&gt;    &amp;lt;appSettings&amp;gt;&lt;br /&gt;&lt;br /&gt;      &amp;lt;add key="myConnInfo" value="server=_;database=_;user=_;pass=_;" /&amp;gt;&lt;br /&gt;&lt;br /&gt;    &amp;lt;/appSettings&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/configuration&amp;gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;p class="ArticleText"&gt;&lt;span&gt;To read the connection setting from the Config file, you have to use a single line of code:&lt;/span&gt;&lt;/p&gt; &lt;p class="ArticleText"&gt;&lt;span&gt;&lt;/span&gt; &lt;/p&gt; &lt;p class="ArticleText"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: 'Courier New';"&gt;System.Configuration.ConfigurationManager.AppSettings("ConnectionInfo")&lt;/span&gt;&lt;/p&gt; &lt;p class="ArticleText"&gt;&lt;span style="font-size: 10pt; font-family: 'Courier New';"&gt;&lt;/span&gt; &lt;/p&gt; &lt;h2 style="color: rgb(102, 51, 0);"&gt;Multiple Config Files&lt;/h2&gt;&lt;span&gt;The appSettings element can contain a file attribute that points to an external file. I will change my web.config file to look like the following:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&amp;lt;?xml version="1.0"?&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;configuration&amp;gt;&lt;br /&gt;&lt;br /&gt;    &amp;lt;appSettings/&amp;gt;&lt;br /&gt;&lt;br /&gt;    &amp;lt;connectionStrings/&amp;gt;&lt;br /&gt;&lt;br /&gt;    &amp;lt;system.web&amp;gt;&lt;br /&gt;&lt;br /&gt;        &amp;lt;compilation debug="false" strict="false" explicit="true" /&amp;gt;&lt;br /&gt;&lt;br /&gt;    &amp;lt;/system.web&amp;gt;&lt;br /&gt;&lt;br /&gt;    &amp;lt;appSettings file="externalSettings.config"/&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/configuration&amp;gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;/span&gt;&lt;p class="ArticleText"&gt;&lt;span&gt;Next, we can create the external file "externalSettings.config" and add an appSettings section with our connection information and any other settings that we want to use.&lt;/span&gt;&lt;/p&gt; &lt;p class="ArticleText"&gt;&lt;span&gt;If the external file is present, ASP.Net combines the appSettings values from web.config with those in the external file. If a key/value exists in both files, then ASP.Net will use the setting from the external file.&lt;/span&gt;&lt;/p&gt; &lt;p class="ArticleText"&gt;&lt;span&gt;This feature is useful when one keeps user-specific or environment-specific settings in the external file. It is better to design web.config to contain those settings that are global, while each user setting is contained in an external file. This approach makes it easier to move around global web.config changes. &lt;/span&gt;&lt;/p&gt; &lt;p class="ArticleText"&gt;&lt;span&gt;One caution to this approach is that ASP.Net runtime does not detect when the external file changes. Thus to launch a new version of the application with all changes in effect, one will need to make changes to the web.config itself. &lt;/span&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2770957963577646170-4924946717158435167?l=ravi-testingblog.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ravi-testingblog.blogspot.com/feeds/4924946717158435167/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ravi-testingblog.blogspot.com/2009/07/include-multiple-config-files-in-aspnet.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2770957963577646170/posts/default/4924946717158435167'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2770957963577646170/posts/default/4924946717158435167'/><link rel='alternate' type='text/html' href='http://ravi-testingblog.blogspot.com/2009/07/include-multiple-config-files-in-aspnet.html' title='Include Multiple .Config Files in ASP.NET Web Application'/><author><name>ravi</name><uri>http://www.blogger.com/profile/07517056757956161890</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2770957963577646170.post-3828668015265620256</id><published>2009-03-12T11:25:00.000-07:00</published><updated>2009-07-28T08:18:30.404-07:00</updated><title type='text'>Enter Key in ASP.NET - Complete Research</title><content type='html'>&lt;span style="font-family: georgia;"&gt;One of the common requests in ASP.NET is to submit a form when visitor hits an Enter key. That could be a case if, for example you want to make Login Screen. It is expected that user just hit enter when he insert a user name and password instead to of forcing him to use a mouse to click login button. If you want to make search function on your web site, it is frequently required to give a possibility to hit enter after you insert a search terms instead of mouse click on a Search button.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;p style="font-family: georgia;"&gt;In HTML or classic ASP pages is not hard to submit forms using the enter key on keyboard. Programmer use a &amp;lt;input type="submit"&amp;gt; to make a default button. If web site visitor click on that button or press enter key, the form will be submited.&lt;/p&gt; &lt;p style="font-family: georgia;"&gt;Of course, you can have a more than one form on your page and individual submit button for every form.&lt;br /&gt;&lt;/p&gt;&lt;h2 style="color: rgb(51, 51, 0); font-family: georgia;"&gt;You don't want to submit a form with Enter key?&lt;/h2&gt;&lt;span style="font-family: georgia;"&gt;Rarely, you will need to disable an Enter key and avoid to submit form. If you want to prevent it completely, you need to use OnKeyDown handler on &amp;lt;body&amp;gt; tag of your page. The javascript code should be:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;blockquote style="font-family: georgia;"&gt;if (window.event.keyCode == 13)&lt;br /&gt;{&lt;br /&gt;   event.returnValue=false;&lt;br /&gt;   event.cancel = true;&lt;br /&gt;}&lt;/blockquote&gt;&lt;br /&gt;&lt;h2 style="font-family: georgia;"&gt;Common ASP.NET problems with Enter key&lt;/h2&gt; &lt;p style="font-family: georgia;"&gt;If you try to use Enter key in ASP.NET, according to your browser's type, you can get really weird results. For example, try to place one ASP.NET textbox and a button to the web form. Write a code on a OnClick event of a button. That could be something simple, like: &lt;/p&gt;&lt;blockquote style="font-family: georgia;"&gt;Response.Write("The button was clicked!");&lt;/blockquote&gt;&lt;br /&gt;&lt;p style="font-family: georgia;" class="ArticleText"&gt;Now start debugging and write something to textbox. If you press enter while focus is on textbox, form will submit, but your code for button's click event will not be executed. &lt;/p&gt; &lt;p style="font-family: georgia;" class="ArticleText"&gt;Stop the debuging and place one more simple HTML textbox to the form. You will not write anything in this textbox, so you can even make it invisible. Just place it somewhere inside of your form tag.&lt;/p&gt;&lt;p style="font-family: georgia;" class="ArticleText"&gt;&lt;br /&gt;&lt;/p&gt;&lt;blockquote style="font-family: georgia;"&gt;&amp;lt;INPUT type="text" style="VISIBILITY: hidden;POSITION: absolute"&amp;gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;p style="font-family: georgia;"&gt;Start debugging again. You cannot see the second textbox, and everything looks like before. Try again to write something in first textbox. If you press enter now, form will submit, and your code for button's click event will now be executed. This is extremely different behavior, and you did nothing except you placed one invisible textbox on web form. :)&lt;/p&gt; &lt;p style="font-family: georgia;"&gt;Maybe it is not best practice, but placing invisible textbox could be simple solution for you if you have only one button on your web form. But, what if you have a different situation? What if you have a few buttons with only one textbox, or more than one text box with only one button, or many text boxes and many buttons with different code for each button, and all that on one form?&lt;/p&gt; &lt;p style="font-family: georgia;"&gt;Different browsers have a different behavior in these cases. In case that you have more buttons, only first button will be "clicked" every time. So, we need some other approach to get an universal solution.&lt;/p&gt;&lt;h2 style="color: rgb(51, 51, 0); font-family: georgia;"&gt;How to make a default button in ASP.NET&lt;/h2&gt;&lt;br /&gt;&lt;span style="font-family: georgia;"&gt;We need to specify exactly which button will be "clicked" when visitor press Enter key, according to which textbox currently has a focus. The solution could be to add onkeydown attribute to textbox control with this code:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;blockquote style="font-family: georgia;"&gt;&lt;strong&gt;TextBox1&lt;/strong&gt;.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('"+&lt;strong&gt;Button1&lt;/strong&gt;.UniqueID+"').click();return false;}} else {return true}; ");&lt;br /&gt;&lt;br /&gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;p style="font-family: georgia;"&gt;This line of code will cause that button Button1 will be "clicked" when visitors press Enter key and cursor is placed in TextBox1 textbox. On this way you can "connect" as many text boxes and buttons as you want. &lt;/p&gt; &lt;h2 style="color: rgb(51, 51, 0); font-family: georgia;"&gt;Easy solution for default button&lt;/h2&gt; &lt;p style="font-family: georgia;"&gt;There is a free component that allows you to assign a button to the "enter-pressed" client side event of input controls. If you type some text in textbox and press Enter, the form will postback, and the serverside click event of your button is fired. You don't need to write any code, but you only need to use this control's "DefaultButton" property. It you are a beginner programmer this could be a life saver. More about MetaBuilders DefaultButtons Control you can find at &lt;a href="http://www.metabuilders.com/Tools/DefaultButtons.aspx"&gt;http://www.metabuilders.com/Tools/DefaultButtons.aspx&lt;/a&gt;&lt;br /&gt;&lt;/p&gt; &lt;h2 style="color: rgb(51, 51, 0); font-family: georgia;"&gt;&lt;br /&gt;Default buttons in ASP.NET 2.0 and ASP.NET 3.5&lt;/h2&gt; &lt;p style="font-family: georgia;"&gt;ASP.NET 2.0 makes this problems easier and introduce a concept of a "default button". New defaultbutton attribute can be used with &amp;lt;form&amp;gt; or &amp;lt;asp:panel&amp;gt; control. What button will be "clicked" depends of where actually cursor is and what button is chosen as a default button for form or a panel.&lt;/p&gt; &lt;p style="font-family: georgia;"&gt;Here is sample HTML code that contains one form and one panel control:&lt;/p&gt;&lt;blockquote style="font-family: georgia;"&gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;blockquote style="font-family: georgia;"&gt;&amp;lt;form defaultbutton="button1" runat="server"&amp;gt;&lt;br /&gt;   &amp;lt;asp:textbox id="textbox1" runat="server"/&amp;gt;&lt;br /&gt;   &amp;lt;asp:textbox id="textbox2" runat="server"/&amp;gt;&lt;br /&gt;   &amp;lt;asp:button id="button1" text="Button1" runat="server"/&amp;gt;&lt;br /&gt;&lt;br /&gt;   &amp;lt;asp:panel defaultbutton="button2" runat="server"&amp;gt;&lt;br /&gt;       &amp;lt;asp:textbox id="textbox3" runat="server"/&amp;gt;&lt;br /&gt;       &amp;lt;asp:button id="button2" runat="server"/&amp;gt;&lt;br /&gt;   &amp;lt;/asp:panel&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/form&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;h2 style="font-family: georgia;"&gt;Last words about Enter&lt;/h2&gt; &lt;p style="font-family: georgia;"&gt;This tutorial covers different cases when you need to manipulate with Enter key. There is &lt;a href="http://www.beansoftware.com/ASP.NET-Tutorials/Access-Tab-Key.aspx"&gt;Tab key in ASP.NET&lt;/a&gt; tutorial, which covers problem with Tab keyboard key. Also, Bean Software offers specialized &lt;a href="http://www.beansoftware.com/Shortcut-Controls/"&gt;Keyboard Shortcut Controls&lt;/a&gt;. With these controls, you can manipulate with different keyboard shortcut without any code. That could be simple shortcuts like Enter, Tab or just placing a focus in text box, or more complicated like Ctrl + Shift + O or similar, just like shortcuts used in Windows applications. Source code is included.&lt;/p&gt; &lt;p style="font-family: georgia;"&gt;Note that all solutions in this tutorial use javascript to submit form when visitor press Enter key on its keyboard. If javascript is disabled in web browser or if visitor uses some very old version, form will not submit. In that (pretty rare) case, user must use a mouse to click on a button. Even Google Search form works on the same way.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2770957963577646170-3828668015265620256?l=ravi-testingblog.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ravi-testingblog.blogspot.com/feeds/3828668015265620256/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ravi-testingblog.blogspot.com/2009/03/test-blog.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2770957963577646170/posts/default/3828668015265620256'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2770957963577646170/posts/default/3828668015265620256'/><link rel='alternate' type='text/html' href='http://ravi-testingblog.blogspot.com/2009/03/test-blog.html' title='Enter Key in ASP.NET - Complete Research'/><author><name>ravi</name><uri>http://www.blogger.com/profile/07517056757956161890</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
