Save yourself hours of debugging time in an ASP.NET web site/app.

If you are working on a web application that has added code to the Global.asax that logs and clears all unhandled exceptions, down the road in the development/maintenance unit test cycle you may forget to check that any errors have occurred during your live testing and can’t figure out why your application isn’t working correctly.  To prevent this from happening to you and other developers on your project add a Debug.Assert message just before the code that logs the unhandled exception. For example:

Debug.Assert(False, String.Format("Exception about to be logged to database:{0}", unhandledException))

Then when you are running in debug mode and an unhandled exception occurs you will get a popup like this:


ASP.NET 2.0 page lifecycle

Every once in a while the ASP.NET 2.0 page lifecycle doesn’t quite work as I thought and I have to re-research it again. Here is a link I found that helped me this time. What was of special note to me this time was that a web user control will not have created its child controls until the page.OnPreRenderComplete event. Which, makes complete sense to me now.

http://blogs.thesitedoctor.co.uk/tim/2006/06/30/Complete+Lifecycle+Of+An+ASPNet+Page+And+Controls.aspx

Fixing HTTP Error 401.1 in IIS 7.5 on Windows 7

For a test I was trying to run a website on my local Windows 7 machine in IIS 7.5.

 I put the website files in my C:\inetpub\wwwroot\MyWebsite. I opened IIS 7.5 and right-clicked on Sites and selected "Add Web Site".

In the "Add Web Site" dialog I entered the "Site name", "Physical path" as above and "Host name" and clicked "OK".

For the "Select Application Pool" dialog I selected "Classic .NET AppPool" which is .Net 2.0 with Classic Pipeline.

I double clicked on "Authentication" and disabled everything and enabled "Windows Authentication" as this is a local intranet website.

In "Application Pools" I went into "Advanced Settings" for the "Classic .NET AppPool" and changed the Identity to "Custom account" and entered "my companies domain\my userid" and password, because that is how the website is run on the corporate server under a special account.

I then went to C:\Windows\System32\drivers\etc and edited my hosts file and added:

::1 MyHostName

Where MyHostName is the name I entered in "Host name" when I first created the website above.

I then opened IE 8 and put http://MyHostName/ in the address box and I get a "Windows Security" dialog box asking for my username and password.  I try and enter "my companies domain\my userid" and password and I just keep getting the same dialog three times and then I get a page that says "Server Error in Application 'SiteName'" where SiteName is the name I entered for "Site name" when I created the website.

On this error page it goes on to say that I got the "HTTP Error 401.2 - Unauthorized" error and "You are not authorized to view this page due to invalid authentication headers."

In the "Detailed Error Information" section it says I got error code 0x80070005.

After much research I discovered I needed to add a Multi-String Value to the following registry key HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0 called BackConnectionHostNames with "MyHostName" as the Value.  As described in "Method 1" here: http://support.microsoft.com/kb/896861.

In my research I did come along a nice blog(http://mvolo.com/troubleshoot-iis7-errors-like-a-pro/) that had some really good information but it didn't have the regedit fix I needed.

And a nice site about all the options with Windows Authentication in IIS can be found here http://www.iis.net/configreference/system.webserver/security/authentication/windowsauthentication



VS 2008 Next Error keyboard shortcut

When I’m refactoring some code if there is an interface change I usually make the interface change first and then fix all the error’s that come up.  Using the mouse to go to the errors window and double click on the next error can get tedious so I did some research to figure out a better way and found there is a keyboard short cut "ctrl-shift-F12" in VS 2008 that will take you to the next error.

For more information on keyboard short cuts check this out:

Microsoft .NET: Architecting Applications for the Enterprise

The book Microsoft® .NET: Architecting Applications for the Enterprise: Architecting Applications for the Enterprise (PRO-Developer) by Dino Esposito and Andrea Saltarello is written for Programmers/Application Architects to use the latest state of the industry .NET techniques and technologies for software development.

They provide very clear explanations of the technologies and techniques like WCF, Entity Framework, Domain Driven Design etc. Excellent examples of when and where to use each technology and technique and when not to use them.

They clearly reference where each technique came from and how to find more information. For me it had just the right amount of code examples and written explanations.  Buy a copy today.

Speed Improvement by avoiding exceptions


I learned a neat thing.  If you press ctrl-alt-e in Visual Studio you bring up this dialog:

Exceptions Dialog

Then check the Thrown checkbox for "Common Language Runtime Exceptions" like this:

Exceptions Dialog with CLR Exceptions Checked

Then run the project in debug mode and if an exception occurs the run will break at that point in the code and you can examine what happened.  This is great for finding uncaught exceptions and improper use of exceptions like this:

                Try
                    If c.Contains("somestring") Then Stop
                Catch ex As NullReferenceException
                End Try

One should never do this because it is extremely inefficient.  Exceptions are costly and should always be avoided if possible.  In this case we can avoid this exception by checking c for nothing, like so:

            If Not IsNothing(c) Then
                If c.Contains("somestring") Then Stop
            End If

VB.NET How to time a section of code.

Once in a while I want to see how long a section of code takes to run and I always forget the best way to do this.

endTime.Subtract(startTime).TotalMilliseconds()

It's so simple it's easy to forget I guess.

How to check if a datetime field equals current date in SQL Server Transact SQL

Had to write a SQL Server query to check if a date is the current day. After some searching I found a good way of doing it here: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=38012

SELECT * FROM table WHERE DATEDIFF(day, StartDate, GETDATE()) = 0