using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Configuration;

public partial class ExecuteReaderAssincrono : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ConnectionStringSettings getString = WebConfigurationManager.ConnectionStrings["nwind"] as ConnectionStringSettings;
        if (getString != null)
        {
            string sSql = "Select ProductName from Products";
            using (SqlConnection conn = new SqlConnection(getString.ConnectionString))
            {
                try
                {
                    SqlCommand command = new SqlCommand(sSql, conn);

                    conn.Open();
                    IAsyncResult result = command.BeginExecuteReader();

                    using (SqlDataReader r = command.EndExecuteReader(result))
                    {
                        Resultado(r);
                    }
                }
                catch (SqlException)
                {
                    Response.Write("Erro SQL.");
                }
                catch (InvalidOperationException)
                {
                    Response.Write("InvalidOperationException");
                }
                catch (Exception)
                {
                    Response.Write("Erro genérico.");
                }
            }
        }
    }

    private static void Resultado(SqlDataReader r)
    {
        int produto = r.GetOrdinal("ProductName");
        while (r.Read())
        {
            HttpContext.Current.Response.Write(r.GetString(produto).ToString() + "<br/>");
        }
    }

}