<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Web.Configuration" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
string connectionString = WebConfigurationManager.ConnectionStrings["AdventureWorks"].ConnectionString;
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string sql = "Select ProductID, Name from Production.Product AS Product " +
"Order by ProductID FOR XML AUTO, ROOT('Products')";
SqlCommand command = new SqlCommand(sql, connection);
XmlReader reader = command.ExecuteXmlReader();
XmlDocument doc = new XmlDocument();
doc.Load(reader);
productsSource.Data = doc.OuterXml;
productsSource.XPath = "Products/Product";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Binding XML Data from Other Sources</title>
</head>
<body>
<form id="form1" runat="server">
<asp:XmlDataSource id="productsSource" runat="server"/>
<asp:GridView ID="productView" Runat="server" DataSourceID="productsSource"
AutoGenerateColumns="False">
<Columns>
<asp:BoundField HeaderText="Product ID" DataField="ProductID"></asp:BoundField>
<asp:BoundField HeaderText="Product Name" DataField="Name"></asp:BoundField>
</Columns>
</asp:GridView>
</form>
</body>
</html>
|