This blog has moved, permanently, to http://software.safish.com.

Friday, June 19, 2009

The ASP.NET Page Life Cycle

The ASP.NET life cycle is as much a fantastic model for web developers as it is a pain the butt. Whenever I change jobs, I encounter different approaches to implementation of pages and controls, rarely with any regard to the correct order of doing things and usually with code that is very difficult to maintain as a result. If you can't rely on your controls to obey the page life cycle in terms of rendering, you've got a problem. For example, setting presentation properties during page load may cause trouble if developers set that property on events firing or during PreRender.

So, when should you do things? As a rule of thumb, I try to do data binding early (OnInit) and presentation setting late (OnPreRender, or in controls that output custom markup in the Render override). It's a tricky topic and something that seems to catch most developers - the concept of top-down development with classic ASP and PHP just doesn't apply.

There's an excellent article on MSDN about the order of events here: http://msdn.microsoft.com/en-us/library/ms178472.aspx.

For my future reference, some items to consider:
  • Init and Load need to be handled carefully: the Init event for controls fires BEFORE the Init event for the Page; the Load event for controls fires AFTER the Load event for the Page.

Tuesday, June 2, 2009

SQL Server Express Remote Connections

I've been doing some work with SQL Server replication over the last few days, and in order to acheive this I was replicating between one of our DB servers and a SQL Server Express instance on my desktop. To my surprise, setting up remote connections to Express is actually a major pain in the butt, so I thought I'd document it here.

Firewall

The first step is to ensure that remote connections are allowed through your firewall. I was using the default Windows firewall, so I set up a new exception: I called it "SQL Server", Port number 1433, Protocol TCP, and I changed the scope to be "My network (subnet) only"

SQL Server Configuration

There are some hidden settings that you need to enable before SQL Server express will allow remote connections. You will need to load up the SQL Server Configuration Manager and do the following:
  • Under SQL Server 2005 Network Configuration, mark the TCP/IP status as Enabled
  • Right-click TCP/IP and go to Properties
  • Go to the IP Addresses tab, and scroll down to IP All
  • Remove the value in TCP Dynamic Ports, and enter "1433" (same value as you used in firewall) in the TCP Port value
SQL Server

The next step is to ensure SQL Server itself allows remote connections. From within SQL Server, go to the SQL Server instance, click Properties, Connections, "Allow remote connections to this server"). Finally, restart the express service and you should be able to connect to your machine's SQL Server instance remotely.