<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head>
<title>DataRow GetChildRows Method</title>
<link rel="stylesheet" href="/shared/netdemos.css">
<style>
.child {font-size:10pt}
.child th {padding:5; background:steelblue; color:mintcream}
.child td {padding:3; background:whitesmoke}
</style>
<script language="C#" runat="server" src="fetchData_sql.cs" />
<script runat="server">
String html;
protected void Page_Load ( Object Src, EventArgs E ) {
if ( !IsPostBack ) {
// fetch data
string query = "SELECT CategoryID, CategoryName FROM Categories; " +
"SELECT CategoryID, ProductName, QuantityPerUnit, UnitPrice, UnitsInStock FROM Products";
DataSet ds = fetchData ( query );
// define a relationship between the two tables
ds.Relations.Add ( "categoryId",
ds.Tables [ 0 ].Columns [ "CategoryID" ], ds.Tables [ 1 ].Columns [ "CategoryID" ] );
// iterate over parent rows ( Categories )
foreach ( DataRow category in ds.Tables [ 0 ].Rows ) {
html += "<div align='center'><h2 style='color:maroon'>" + category [ "CategoryName" ] + "</h2></div>";
// begin table headers markup
html += "<table class='child' width=70% align='center' cellspacing=1>";
html += "<tr><th>Product Name</th>";
html += "<th>Packaging</th>";
html += "<th>Unit Price</th>";
html += "<th>In Stock</th></tr>";
// iterate over child rows ( Products )
foreach ( DataRow product in category.GetChildRows ( "categoryId" ) ) {
html += "<tr><td>" + product [ "ProductName" ] + "</td>";
html += "<td>" + product [ "QuantityPerUnit" ] + "</td>";
html += "<td align='right'>" +
String.Format ( "{0:f2}", product [ "UnitPrice" ] ) + "</td>";
html += "<td align='right'>" + product [ "UnitsInStock" ] + "</td></tr>";
}
html += "</table>";
}
}
}
</script>
</head>
<body>
<!-- #include virtual="~/shared/top.inc" -->
<div class="header"><h2>DataRow <span class="hilite">GetChildRows</span> Method</h2></div>
<!-- #include virtual="~/shared/viewsrc_top.inc" -->
<hr size=1 width=92%>
<%= html %>
<hr size=1 width=92%>
<!-- #include virtual="~/shared/viewsrc.inc" -->
</body>
</html>