DATAREADER

Categories:

Products:
ProductIDProductNameUnitPriceUnitsInStock
1notebook1500.000017
2Coca-Cola 1114.00004
24Guaraná Fantástica4.500020
34Sasquatch14.0000111
35Steeleye Stout18.000020
38Côte de Blaye263.500017
39Chartreuse verte18.000069
43Ipoh Coffee46.000017
67Laughing Lumberjack Lager14.000052
70Outback Lager100.000015
75Rhönbräu Klosterbier7.7500125
76Lakkalikööri18.000057

Show code / exibir código
using System.Data.SqlClient;
using System.Web.Configuration;

string conStr = WebConfigurationManager.ConnectionStrings["ConnStringKey"].ConnectionString;

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        string sql = "Select CategoryID, CategoryName FROM Categories ORDER BY CategoryName";
        SqlConnection conn = new SqlConnection(conStr);
        SqlCommand cmd = new SqlCommand(sql, conn);
        try
        {
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            dropCategories.DataSource = reader;
            dropCategories.DataValueField = "CategoryID";
            dropCategories.DataTextField = "CategoryName";
            dropCategories.DataBind();
            reader.Close();
            dropCategories_SelectedIndexChanged(sender, e);
        }
        finally
        {
            conn.Close();
        }
    }
}

protected void dropCategories_SelectedIndexChanged(object sender, EventArgs e)
{
    string sql = "Select ProductID, ProductName, UnitPrice, UnitsInStock FROM Products Where CategoryID=@cat";
    SqlConnection conn = new SqlConnection(conStr);
    SqlCommand cmd = new SqlCommand(sql, conn);
    SqlParameter p1 = new SqlParameter("@cat", SqlDbType.Int);
    p1.Value = dropCategories.SelectedItem.Value;
    cmd.Parameters.Add(p1);
    try
    {
        conn.Open();
        SqlDataReader reader = cmd.ExecuteReader();
        griddata.DataSource = reader;
        griddata.DataBind();
        reader.Close();
    }
    finally
    {
        conn.Close();
    }
}