List Binding To A DataSet : DataSet « ADO.net Database « ASP.NET Tutorial






<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">

    DataSet myDataSet = new DataSet();

    void Page_Load(object sender, EventArgs e)
    {
       string ConnectionString = Convert.ToString(ConfigurationSettings.AppSettings["MSDEConnectString"]);
       string CommandText = "SELECT * FROM Publisher";

       SqlConnection myConnection = new SqlConnection(ConnectionString);
       SqlCommand myCommand = new SqlCommand(CommandText, myConnection);

       SqlDataAdapter myAdapter = new SqlDataAdapter();

       myAdapter.SelectCommand = myCommand;

       try {
          myConnection.Open();

          myAdapter.Fill(myDataSet, "Publisher");
       } catch (Exception ex) {
          throw (ex);
       } finally {
          myConnection.Close();
       }
       Page.DataBind();

    }

</script>
<html>
<body>
    <form runat="server">
        <h2>A CheckBoxList</h2>
        
            <asp:CheckBoxList id="CheckBoxList1" runat="server" BorderWidth="2px" BorderStyle="Dotted" DataValueField="PublisherID" DataTextField="PublisherName" DataSource='<%# myDataSet.Tables["Publisher"] %>'></asp:CheckBoxList>
        
        <h2>A RadioButtonList</h2>
        
            <asp:RadioButtonList id="RadioButtonList1" runat="server" DataValueField="PublisherID" DataTextField="PublisherName" DataSource='<%# myDataSet.Tables["Publisher"] %>'></asp:RadioButtonList>
        
        <h2>A DropDownList</h2>
        
            <asp:DropDownList id="DropDownList1" runat="server" DataValueField="PublisherID" DataTextField="PublisherName" DataSource='<%# myDataSet.Tables["Publisher"] %>'></asp:DropDownList>
        
        <h2>A ListBox </h2>
        
            <asp:ListBox id="ListBox1" runat="server" DataValueField="PublisherID" DataTextField="PublisherName" DataSource='<%# myDataSet.Tables["Publisher"] %>' 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.27.DataSet
18.27.1.The DataSet object represents an in-memory database.
18.27.2.Fill a DataSet
18.27.3.Iterating Through A DataSet
18.27.4.Fill DataSet with SqlDataAdapter
18.27.5.Use OleDbDataAdapter to fill DataSet
18.27.6.Iterating Through A DataSet from MySQL database
18.27.7.List Binding To A DataSet
18.27.8.Pulling Single Values From Dataset Bounded Lists
18.27.9.Create DataSet by your own
18.27.10.Serialization capabilities of DataSet