Fill a DataSet : DataSet « ADO.net Database « ASP.NET Tutorial






<%@ Page language="c#" Inherits="FillDataSet" CodeFile="Default.aspx.cs" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
  <HEAD>
    <title>FillDataSet</title>
    </HEAD>
  <body>
    <form id="Form1" method="post" runat="server">
      <TABLE id="Table1" width="100%">
        <TR>
          <TD>
            <asp:Repeater id="Repeater1" runat="server">
              <HeaderTemplate>
                <h2>Repeater</h2>
              </HeaderTemplate>
              <ItemTemplate>
                <li>
                  <%# DataBinder.Eval(Container.DataItem, "TitleOfCourtesy") %>
                  <b>
                    <%# DataBinder.Eval(Container.DataItem, "LastName") %>
                  </b>,
                  <%# DataBinder.Eval(Container.DataItem, "FirstName") %>
                </li>
              </ItemTemplate>
            </asp:Repeater></TD>
          <TD>
            <H2>foreach approach</H2>
            <asp:Literal id="HtmlContent" runat="server"></asp:Literal></TD>
        </TR>
      </TABLE>
    </form>
  </body>
</HTML>

File: Default.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Data.SqlClient;

public partial class FillDataSet : System.Web.UI.Page
{

  protected void Page_Load(object sender, System.EventArgs e)
  {
    string connectionString = "Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI";
    SqlConnection con = new SqlConnection(connectionString);
    string sql = "SELECT * FROM Employees";

    SqlDataAdapter da = new SqlDataAdapter(sql, con);
    DataSet ds = new DataSet();

    da.Fill(ds, "Employees");

    StringBuilder htmlStr = new StringBuilder("");
    foreach (DataRow dr in ds.Tables["Employees"].Rows)
    {
      htmlStr.Append("<li>");
      htmlStr.Append(dr["TitleOfCourtesy"].ToString());
      htmlStr.Append(" <b>");
      htmlStr.Append(dr["LastName"].ToString());
      htmlStr.Append("</b>, ");
      htmlStr.Append(dr["FirstName"].ToString());
      htmlStr.Append("</li>");
    }

    HtmlContent.Text = htmlStr.ToString();

    Repeater1.DataSource = ds;
    Repeater1.DataMember = "Employees";
    Repeater1.DataBind();

  }
}








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