Pulling Single Values From Dataset Bounded Lists : 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 BuildDataSetTable(string commandText, string tableName)
    {
       string ConnectionString = Convert.ToString(ConfigurationSettings.AppSettings["MSDEConnectString"]);

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

       SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);

       try {
          myConnection.Open();
          myAdapter.Fill(myDataSet, tableName);
       } catch (Exception ex) {
          throw(ex);
       } finally {
          myConnection.Close();
       }
    }

    void Page_Load(object sender, EventArgs e) {
       if (!(Page.IsPostBack)) 
       {
          string SelectPublisher = "SELECT PublisherID, PublisherName From Publisher";
          BuildDataSetTable(SelectPublisher, "Publisher");

          Label1.Text = "Select a Publisher";

          DropDownList1.DataBind();
          RadioButtonList1.DataBind();
          ListBox1.DataBind();
       }
    }

    void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e) {
          string SelectBook = "SELECT * FROM Book WHERE BookPublisherID=" + RadioButtonList1.SelectedItem.Value;
          BuildDataSetTable(SelectBook, "Book");
          DataGrid1.DataBind();
    }

    void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) {
          string SelectBook = "SELECT * FROM Book WHERE BookPublisherID=" + DropDownList1.SelectedItem.Value;
          BuildDataSetTable(SelectBook, "Book");
          DataGrid1.DataBind();
    }

    void ListBox1_SelectedIndexChanged(object sender, EventArgs e) {
          string SelectBook = "SELECT * FROM Book WHERE BookPublisherID=" + ListBox1.SelectedItem.Value;
          BuildDataSetTable(SelectBook, "Book");
          DataGrid1.DataBind();
    }

</script>
<html>
<head>
</head>
<body>
    <form runat="server">
    
        <asp:RadioButtonList id="RadioButtonList1" runat="server" DataSource='<%# myDataSet.Tables["Publisher"] %>' DataTextField="PublisherName" DataValueField="PublisherID" AutoPostBack="True" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged"></asp:RadioButtonList>
    
    <h2>A DropDownList
    </h2>
    
        <asp:DropDownList id="DropDownList1" runat="server" DataSource='<%# myDataSet.Tables["Publisher"] %>' DataTextField="PublisherName" DataValueField="PublisherID" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList>
    
    <h2>A ListBox
    </h2>
    
        <asp:ListBox id="ListBox1" runat="server" DataSource='<%# myDataSet.Tables["Publisher"] %>' DataTextField="PublisherName" DataValueField="PublisherID" AutoPostBack="True" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"></asp:ListBox>
    
    
        <asp:Label id="Label1" runat="server"></asp:Label>
    
    
        <asp:DataGrid id="DataGrid1" runat="server" DataSource='<%# myDataSet.Tables["Book"] %>' BorderStyle="None" BorderWidth="1px" BorderColor="#CC9966" BackColor="White" CellPadding="4">
            <FooterStyle forecolor="#330099" backcolor="#FFFFCC"></FooterStyle>
            <HeaderStyle font-bold="True" forecolor="#FFFFCC" backcolor="#990000"></HeaderStyle>
            <PagerStyle horizontalalign="Center" forecolor="#330099" backcolor="#FFFFCC"></PagerStyle>
            <SelectedItemStyle font-bold="True" forecolor="#663399" backcolor="#FFCC66"></SelectedItemStyle>
            <ItemStyle forecolor="#330099" backcolor="White"></ItemStyle>
        </asp:DataGrid>
    
    </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