<%@ 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();
}
}
|