List Binding DataReader : DataReader « ADO.net Database « ASP.NET Tutorial






<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">
    SqlDataReader myReader;
    
    void Page_Load(object sender, EventArgs e)
    {
       string ConnectionString = Convert.ToString(ConfigurationSettings.AppSettings["MSDEConnectString"]);
       string CommandText = "SELECT PublisherID, PublisherName FROM Publisher";
    
       SqlConnection myConnection = new SqlConnection(ConnectionString);
       SqlCommand myCommand = new SqlCommand(CommandText, myConnection);
    
       try
       {
          myConnection.Open();
    
          myReader = myCommand.ExecuteReader();
    
          ListBox1.DataBind();
          myReader.Close();
       }catch (Exception ex) {
          throw (ex);
       } finally {
          myConnection.Close();
       }
    }
</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <h1>Pulling Values From Data Bound Lists&nbsp;
        </h1>
        <h2>A CheckBoxList
        </h2>
        
            <asp:CheckBoxList id="CheckBoxList1" runat="server" BorderWidth="2px" BorderStyle="Dotted" DataValueField="PublisherID" DataTextField="PublisherName" DataSource="<%# myReader %>"></asp:CheckBoxList>
        
        <h2>A RadioButtonList
        </h2>
        
            <asp:RadioButtonList id="RadioButtonList1" runat="server" DataValueField="PublisherID" DataTextField="PublisherName" DataSource="<%# myReader %>"></asp:RadioButtonList>
        
        
        
        <h2>A DropDownList
        </h2>
        
            <asp:DropDownList id="DropDownList1" runat="server" DataValueField="PublisherID" DataTextField="PublisherName" DataSource="<%# myReader %>"></asp:DropDownList>
        
        
        
        <h2>A ListBox
        </h2>
        
            <asp:ListBox id="ListBox1" runat="server" DataValueField="PublisherID" DataTextField="PublisherName" DataSource="<%# myReader %>" Rows="5"></asp:ListBox>
        
    </form>
</body>
</html>



File: Web.config

<configuration>
    <appSettings>
        <add key="MSDEConnectString" value="server=(local)\YourDatabase;database=Books;uid=YourID;pwd=letmein;" />
    </appSettings>
</configuration>








18.24.DataReader
18.24.1.Using the DataReader Object
18.24.2.Iterating Through A DataReader
18.24.3.Create DataReader object from SqlCommand
18.24.4.List Binding DataReader
18.24.5.Returning Multiple Resultsets
18.24.6.Working with Multiple Active Resultsets