Viewing source for Recipe1104cs.aspx
<%@ Page Language="C#" ClassName="Recipe1202cs" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
//object vars
SqlConnection sqlConnection;
SqlDataAdapter sqlDataAdapter;
SqlCommand sqlCommand;
SqlParameter sqlParameter;
DataSet dataSet;
DataTable dataTable;
try
{
sqlConnection = new SqlConnection( "Integrated Security=yes;Initial Catalog=Northwind;Data Source=(local)" );
//pass the stored proc name and SqlConnection
sqlCommand = new SqlCommand( "SelectCustomersAndOrders", sqlConnection );
//important to set this as StoredProcedure is *not* the default
sqlCommand.CommandType = CommandType.StoredProcedure;
//instantiate SqlAdapter and DataSet
sqlDataAdapter = new SqlDataAdapter( sqlCommand );
dataSet = new DataSet();
//populate the DatSet
sqlDataAdapter.Fill( dataSet, "MyTable" );
//bind first table the to the first DataGrid
//you could also use dataSet.Tables["MyTable"];
dataGrid1.DataSource = dataSet.Tables[0];
//bind the second table to the 2nd DataGrid
//Alternatively, we could use dataSet.Tables("1");
dataGrid2.DataSource = dataSet.Tables["MyTable1"];
//bind the Page's data
DataBind();
}
catch( Exception exception )
{
errorMsgLabel.Text = exception.ToString();
}
}
</script>
<html>
<head>
<title>Create a Multi-Table DataSet from a Stored Procedure</title>
</head>
<body>
<form id="Recipe1104" method="post" runat="server">
<asp:DataGrid id="dataGrid1" runat="server" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="4">
<selecteditemstyle font-bold="True" forecolor="#663399" backcolor="#FFCC66"></selecteditemstyle>
<itemstyle forecolor="#330099" backcolor="White"></itemstyle>
<headerstyle font-bold="True" forecolor="#FFFFCC" backcolor="#990000"></headerstyle>
<footerstyle forecolor="#330099" backcolor="#FFFFCC"></footerstyle>
<pagerstyle horizontalalign="Center" forecolor="#330099" backcolor="#FFFFCC"></pagerstyle>
</asp:DataGrid>
<br />
<asp:DataGrid id="dataGrid2" runat="server" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="4">
<selecteditemstyle font-bold="True" forecolor="#663399" backcolor="#FFCC66"></selecteditemstyle>
<itemstyle forecolor="#330099" backcolor="White"></itemstyle>
<headerstyle font-bold="True" forecolor="#FFFFCC" backcolor="#990000"></headerstyle>
<footerstyle forecolor="#330099" backcolor="#FFFFCC"></footerstyle>
<pagerstyle horizontalalign="Center" forecolor="#330099" backcolor="#FFFFCC"></pagerstyle>
</asp:DataGrid>
<br />
<asp:Label id="errorMsgLabel" runat="server" Width="327px" Height="111px"></asp:Label>
</form>
</body>
</html>