01.Blogs :
travisowens  

Never trust a default!

Never trust a default!

One concept I've been practicing for a few years (that I learned from HTML) is that you should never trust a default.  It doesn't matter if you're writing an HTML page and skipping the BGCOLOR and TEXT tags of a <body> tag (orr the CSS equivilant) or writing C# code and not closing (or possibly even disposing) objects and database connections.

What caused to post this is that in my ADO.Net class retake today, one guy mentioned he learned the hard way in .Net that many times if you don't close your connection object, the connection will stay open, even after the ASP.Net process is finished.  While garbage collection may clean this stuff up eventually, I find it a good practice to never trust anything, especially when it affects speed/usability.

I first learned this when I stopped setting the tags in HTML's <body> because in IE, the background was always white and the text was always black, but of course let's not forget the user can change their default and Mac IE displays grey as the default background color.  Plus many other factors.

It's better safe than sorry, you may have to type a little bit more here and there, but at least there will never be a time when things don't work as you planned and you spend hours figuring out why.

posted on Monday, April 12, 2004 8:57 PM by travisowens

# @ Monday, April 12, 2004 11:02 PM

Just to be fair...ADO.NET wants you to close the connection as soon as you are done with it. If you never close it, then it's not really reasonable to expect it to magically know when you're done with it. That's why I always open just before executing the statement and then close right after OR give the DataReader the command to close it with CommandBehavior.CloseConnection. That way, I know exactly when my connection is closed and there aren't any problems. I guess it depends on the situation though, because I can see the need sometimes to leave a connection open while you do lots of stuff that is a single transaction, but still, you should close the connection as soon as the transaction is done if nothing else. I totally agree with you about never trusting defaults though. I wish it were true that defaults really were defaults everywhere they were supposed to be, but I guess that would make things too easy for us programmers. :)

AdamMB


 
03.UPDATE CALENDAR :
<April 2004>
SunMonTueWedThuFriSat
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678

05.MY LINKS :

07.Subscriptions :

Subscriptions


© Copyright 2005 Microsoft Corporation. All Rights Reserved.
Terms of Use | Privacy Statement | Code of Conduct | Hosted by MaximumASP for Microsoft
WHO-BAR