Thursday, October 29, 2009

Site Under Maintenance with ASP.Net 2.0

If you have worked with deploying websites onto production/test environments, you might have wanted to show a 'Site Under Maintenance' or a similar message to all those who tries to access the website. To achieve this, people used to resort to all kinds of  setups ranging from HttpModules to setting up a html page as default page of the application.

Now in Asp.Net 2.0 onwards we have a simple way of acheiving this. Just put a file NAMED app_offline.htm in the root folder of the application and : voilĂ !! all new requests will be redirected to that html page. 

To revert back, just rename the file to some other name. Neat & clean. 


[Edit 04 Aug 2010 ] This might not be good idea as ASP.Net responds with a 404 http code. Ideally it should have been 200.

Monday, October 12, 2009

Simple way to check time consumed by a piece of code using stopwatch class

TimeSpan ts;
string elapsedTime;
System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
stopWatch.Start();
//DO SOME LENGTHY OPERATION
ts = stopWatch.Elapsed;
elapsedTime =
                    String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                    ts.Hours, ts.Minutes, ts.Seconds,
                    ts.Milliseconds / 10);
 stopWatch.Reset();
 stopWatch.Stop();

Use XML with SQL

Declare @doc xml

set @doc ='


'
Declare @handle int

Exec sp_xml_prepareDocument @handle output, @doc

Select ImageName, date From openXML(@handle, N'/r/i') with (ImageName varchar(20) 'n', date datetime 'm')
Select ImageName,date From openxml(@handle,'/r/t') with (ImageName varchar(20) 'n', date datetime 'm')

Exec sp_xml_removeDocument @handle