Categories: Blog

Space Shuttle Discovery and Async Pages

Wow! I just took a break from work and watched the space shuttle launch. Amazing. Beautiful. Awesome. Words can’t do it justice. My wife and kids went to TechEd with me this year, and while we were there, we toured the Kennedy Space Center and got a close-up look at the two space shuttle launch pads (which happen to be the same pads used for the Apollo moon missions). It made this morning’s launch more exciting.

If you watched the launch on Fox News, you saw a shuttle astronaut named Tom Jones providing commentary. Tom has flown on four shuttle missions. My son and I met him and had our picture taken with him last month at Kennedy Space Center. Tom gave us a first-hand account of what the first couple of minutes after launch is like. (Lots of shaking and rattling.)

On a slightly different subject, this weekend I put the finishing touches on the final edits to October’s Wicked Code column. The subject: asynchronous pages in ASP.NET 2.0. There’s very little documentation on the subject in beta 2 (or in the trade magazines), but async pages are ultra-cool and are something EVERY ASP.NET developer should know about. Here’s the code-behind class for an async page that uses async ADO.NET to do asynchronous data binding:

public partial class AsyncDataBind : System.Web.UI.Page
{
    private SqlConnection _connection;
    private SqlCommand _command;
    private SqlDataReader _reader;
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // Hook PreRenderComplete event for data binding
            this.PreRenderComplete += new EventHandler(Page_PreRenderComplete);

            // Register async methods
            AddOnPreRenderCompleteAsync(
                new BeginEventHandler(BeginAsyncOperation),
                new EndEventHandler(EndAsyncOperation)
            );
        }
    }

    IAsyncResult BeginAsyncOperation (object sender, EventArgs e, AsyncCallback cb, object state)
    {
        string connect = WebConfigurationManager.ConnectionStrings[“PubsConnectionString”].ConnectionString;
        _connection = new SqlConnection(connect);
        _connection.Open();
        _command = new SqlCommand(“SELECT title_id, title, price FROM titles”, _connection);
        return _command.BeginExecuteReader (cb, state);
    }

    void EndAsyncOperation(IAsyncResult ar)
    {
        _reader = _command.EndExecuteReader(ar);
    }

    protected void Page_PreRenderComplete(object sender, EventArgs e)
    {
        Output.DataSource = _reader;
        Output.DataBind();
    }

    public override void Dispose()
    {
        if (_connection != null)
            _connection.Close();
        base.Dispose();
    }
}

That’s just one way to leverage the new async features of Whidbey. Lots more in October’s Wicked Code.

Jeff Prosise

Recent Posts

8-Step AWS to Microsoft Azure Migration Strategy

Microsoft Azure and Amazon Web Services (AWS) are two of the most popular cloud platforms.…

3 weeks ago

How to Navigate Azure Governance

 Cloud management is difficult to do manually, especially if you work with multiple cloud…

4 weeks ago

Why Azure’s Scalability is Your Key to Business Growth & Efficiency

Azure’s scalable infrastructure is often cited as one of the primary reasons why it's the…

1 month ago

Unlocking the Power of AI in your Software Development Life Cycle (SDLC)

https://www.youtube.com/watch?v=wDzCN0d8SeA Watch our "Unlocking the Power of AI in your Software Development Life Cycle (SDLC)"…

2 months ago

The Role of FinOps in Accelerating Business Innovation

FinOps is a strategic approach to managing cloud costs. It combines financial management best practices…

2 months ago

Azure Kubernetes Security Best Practices

Using Kubernetes with Azure combines the power of Kubernetes container orchestration and the cloud capabilities…

2 months ago