C# • Create a Data-Driven Web App

Listing 1. ASP.NET 2.0 streamlines data-driven application development. This example uses the categories and product tables in SQL Server's Northwind sample database, displaying the categories in a dropdown list. This code listing uses the selected category in the dropdown list to display all the products in that category in a GridView control.

<%@ page language="C#" %>
<html>
<head runat="server">
		<title>SqlDataSource 
		Example
		</title>
</head>
<body>
<form runat="server">			
	<asp:sqldatasource 
		id="categoriesDataSource"	
		runat="server" 
		datasourcemode="DataSet" 
		connectionstring="Data 
		Source=localhost;uid=sa;
		pwd=;Initial 
		Catalog=Northwind;"
		selectcommand="usp_GetCategories">
	</asp:sqldatasource>
	Select Categories:
	<asp:dropdownlist 
		id="ddlCategoriesList" 
		runat="server" 
		datavaluefield="CategoryID" 
		datasourceid="categoriesDataSource"	
		autopostback="true"
		datatextfield="CategoryName">
	</asp:dropdownlist>
	<asp:sqldatasource runat="server" 
		id="productsDataSource" 
		datasourcemode="DataReader"
		connectionstring="Data
		Source=localhost;uid=sa;pwd=;
		Initial Catalog=Northwind;"
		selectcommand="SELECT ProductID, 
		ProductName, QuantityPerUnit, 
		UnitPrice  FROM Products Where 
		CategoryID=@CategoryID">
		<selectparameters>
		<asp:controlparameter 
			defaultvalue="1" 
			name="CategoryID" 
			propertyname="SelectedValue" 
			controlid="ddlCategoriesList">
		</asp:controlparameter>
		</selectparameters>
	</asp:sqldatasource>
	<br/><b><br />
	Product Details<br/></b><br />
	<asp:gridview 
		id="productsGrid" runat="server" 
		datasourceid="productsDataSource">
	</asp:gridview>
</form>
</body>
</html>