ASP.NET Tip-O-Rama, Part 2

Because you can never have too many troubleshooting tips and tricks.

Don Kiely

October 30, 2009

3 Min Read
ITPro Today logo in a gray background | ITPro Today

Troubleshooting Tips

LANGUAGES: All .NET Languages

ASP.NET VERSIONS: 1.0 | 1.1

 

ASP.NET Tip-O-Rama, Part 2

Because you can never have too many troubleshooting tipsand tricks.

 

By Don Kiely

 

In the last issue of my column in asp.netNOW (http://www.aspnetpro.com/art.asp?id=314)I provided tips based on questions I frequently see on some of the ASP.NETforums I monitor. But that, my friends, was just the beginning. Here are abunch more.

 

Session ObjectsAway from Pages: You can access the Session object from a class thatdoesn't inherit from the Page class this way:

 

Imports System.Web

HttpContext.Current.Session("mySessionVariable")

 

Web forms have this built into them already, which is whyyou don't need to add this code in a regular code behind module.

 

Add a Windows Loginto MSDE (or SQL Server): Here's how to add the existing ASPNET Windows userto allow access to the db (change the db name, etc., of course):

 

osql -E -S MachineNameNetSDK -Q "sp_grantlogin'MachineNameASPNET'"

osql -E -S MachineNameNetSDK -d NameOfDb -Q"sp_grantdbaccess 'MachineNameASPNET'"

osql -E -S MachineNameNetSDK -d NameOfDb -Q"sp_addrolemember 'db_owner', 'MachineNameASPNET'"

 

Administering MSDEWithout Enterprise Manager: MSDE is a great database for some Webapplications, but it doesn't come with GUI tools to manage it, and osql fromthe command line can be tedious. ASP.NET Enterprise Manager is an open sourceSQL Server and MSDE management tool. Microsoft's ASP.NET Web Matrix includes adatabase management tool. Microsoft's Web Data Administrator is a freeWeb-based MSDE management program written using C# and ASP.NET, and includessource code. You can also access MSDE using Access.

 

No Stinkin' BackButton! If your app just can't tolerate a user clicking the back button,you can tell the browser to not cache the page. Most of the time it will listenif you use this code, which covers all the bases:

 

Response.Expires = 60

Response.ExpiresAbsolute = DateAdd(DateInterval.Day, -1, Now())

Response.AddHeader("pragma", "no-cache")

Response.AddHeader("cache-control","private")

Response.CacheControl = "no-cache"

 

You can do this all programmatically as well, to customizethe behavior of the page, such as to show a message that something has expired.

 

Cache Cleaning Day:Sometimes VS .NET's VSWebCache directory gets out of sync with reality. You canclean it out when things just don't seem be working right in VS .NET. Close VS.NET, go to C:Documents and Settings[userlogin]VSWebCache[machineName], anddelete the subdirectory that corresponds to the project. When you start VS .NETagain, it will recreate it. You should also clean out the directory atC:WINDOWSMicrosoft.NETFrameworkv1.1.4322Temporary ASP.NET Files (this pathmay be a bit different depending on your Windows directory name and the versionof the framework you're running).

 

ASP.NET Recycling:Recycling is usually good and will save the planet, but ASP.NET recycling canbe bad. Sometimes you get this message in your event logs:

 

"aspnet_wp.exe ... was recycled because it was suspectedto be in a deadlocked state"

 

ASP.NET has a feature that, if it suspects that a processis deadlocked, restarts itself. This makes the service a lot more robust, butit can also be triggered by processes that take a long time to generate therequested page. You have two choices, in most cases. One, and this is usuallythe best one, is to simplify the page processing so that it doesn't take solong. This sometimes isn't feasible for all situations, such as long,complicated database queries, but there are ways around it. The other option isto change the executionTimeout setting in the element inmachine.config. It's probably set to 180 seconds, although Framework updatesseem to change that to 90 seconds. You can increase that value, but keep inmind that doing so will increase it for allASP.NET apps on that machine. If an ASP.NET app really does freeze or die,it'll take that much longer for ASP.NET to restart itself.

 

If you have any tips or some refinements of these, pleaselet me know. Or, better yet, come visit the Microsoft-sponsored ASP.NETcommunity at http://www.asp.net and sharethem there!

 

Don Kiely is seniortechnology consultant for Information Insights, a business and technologyconsultancy in Fairbanks, Alaska. E-mail him at mailto:[email protected].

 

 

 

 

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like