Step 6: add custom encoding to the DAL
TableAdapter and able added to a strongly typed DataSet are defined in an XML Schema definition file (Northwind. xsd. In Solution Explorer, you can right-click the Northwind. xsd file and select View Code to open the Schema file.
Figure 32: XML Schema definition file of the Northwinds strong-type DataSet
This schema information will be translated into C # Or Visual Basic Encoding after compilation at design, or translated at runtime if necessary, then you can traverse and execute in a single step in the debugger. To view the automatically generated codes, expand the TableAdapter class or strong DataSet class in the Class View. If you cannot see the Class View on the screen, select Class View From the View menu or press Ctrl + Shift + C. In the class view, you can see the properties, methods, and events of the strong DataSet class and TableAdapter class. To see the encoding of a specific method, double-click the name of the corresponding method in the Class View or right-click the method and select "Go To Definition )".
Figure 33: Select "Go To Definition" in the Class View To view the automatically generated Encoding
Although the automatically generated encoding saves time and effort, such encoding is often very generic. To meet the unique needs of an application, you need to customize the encoding. But the risk of automatically generated extended encoding is that if the tool that generates these encodings decides that it is time to re-generate these encodings, it will overwrite your custom encoding. With the concept of a new part (partial) class in. NET 2.0, it is easy to write the definition of a class in several files. This allows us to add our own methods, attributes, and events to automatically generated classes without worrying that Visual Studio will rush out our custom encoding.
To demonstrate how to customize the DAL, let's add a GetProducts () method to SuppliersRow. This SuppliersRow class represents the individual records of the Suppliers table. Each supplier (supplier) can provide 0 to multiple products, so GetProducts () will return the products of the specified supplier. Add a new class file in the App_Code folder, name it SuppliersRow. cs, and then add the following encoding:
C #
- using System;
- using System.Data;
- using NorthwindTableAdapters;
-
- public partial class
-
- Northwind
- {
- public partial class
-
- SuppliersRow
- {
- public Northwind.ProductsDataTable GetProducts()
- {
- ProductsTableAdapter productsAdapter =
- new ProductsTableAdapter();
- return
- productsAdapter.GetProductsBySupplierID(this.SupplierID);
- }
- }
- }
-
This part (partial) Class instructs the compiler to include the GetProducts () method we just defined when compiling the Northwind. SuppliersRow class. If you compile your project and return to the Class View, you will see that GetProducts () has been listed as a method of Northwind. SuppliersRow.
Figure 34: Custom encoding: The GetProducts () method is part of the Northwind. SuppliersRow class.
The GetProducts () method can now be used to enumerate a list of products from a specified supplier, as shown in the following code:
C #
- NorthwindTableAdapters.SuppliersTableAdapter
-
- suppliersAdapter = new
-
- NorthwindTableAdapters.SuppliersTableAdapter();
-
- // Get all of the suppliers
- Northwind.SuppliersDataTable suppliers =
- suppliersAdapter.GetSuppliers();
-
- // Enumerate the suppliers
- foreach (Northwind.SuppliersRow supplier in suppliers)
- {
- Response.Write("Supplier: " +
-
- supplier.CompanyName);
- Response.Write("");
-
- // List the products for this supplier
- Northwind.ProductsDataTable products = supplier.GetProducts();
- foreach (Northwind.ProductsRow product in products)
- Response.Write("" +
-
- product.ProductName + "");
-
- Response.Write(" ");
- }
-
This data can also be displayed in any of asp.net's data Web controls. the following page uses a GridView control with two fields: data can also be displayed in any asp.net Web control. The following webpage uses the GridView control with two fields:
A BoundField is used to display the name of each supplier,
Another TemplateField contains a BulletedList control to bind the results returned by the GetProducts () method called for each vendor
We will discuss how to display such a master/Slave report in a later tutorial. The purpose of this example is to demonstrate how to use a custom method added to the Northwind. SuppliersRow class.
SuppliersAndProducts. aspx
Asp.net
- < %@ Page Language="C#"
-
- AutoEventWireup="true" CodeFile="SuppliersAndProducts.aspx.cs"
-
- Inherits="SuppliersAndProducts" %>
-
- < !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
-
- Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
- < html xmlns="http://www.w3.org/1999/xhtml" >
- < head runat="server">
- < title>Untitled Pagetitle>
- < link href="Styles.css"
-
- rel="stylesheet"
-
- type="text/css"
-
- />
- head>
- < body>
- < form id="form1" runat="server">
- < div>
- < h1>
- Suppliers and Their Productsh1>
- < p>
- < asp:GridView ID="GridView1" runat="server"
- AutoGenerateColumns="False"
- CssClass="DataWebControlStyle">
- < HeaderStyle CssClass="HeaderStyle" />
- < AlternatingRowStyle CssClass="AlternatingRowStyle" />
- < Columns>
- < asp:BoundField DataField="CompanyName"
- HeaderText="Supplier" />
- < asp:TemplateField HeaderText="Products">
- < ItemTemplate>
- < asp:BulletedList ID="BulletedList1"
- runat="server" DataSource="< %#
- ((Northwind.SuppliersRow)((System.Data.DataRowView)
- Container.DataItem).Row).GetProducts() %>"
- DataTextField="ProductName">
- asp:BulletedList>
- ItemTemplate>
- asp:TemplateField>
- Columns>
- asp:GridView>
- p>
-
- div>
- form>
- body>
- html>
SuppliersAndProducts. aspx. cs
C #
- using System;
- using System.Data;
- using System.Configuration;
- using System.Collections;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using NorthwindTableAdapters;
-
- public partial class
-
- SuppliersAndProducts : System.Web.UI.Page
- {
- protected void
-
- Page_Load(object sender, EventArgs e)
- {
- SuppliersTableAdapter suppliersAdapter = new
- SuppliersTableAdapter();
- GridView1.DataSource = suppliersAdapter.GetSuppliers();
- GridView1.DataBind();
- }
- }
-
Figure 35: Custom encoding: the vendor's company name is listed on the left and their products are listed on the right
Summary
When constructing a web application, creating the DAL should be one of the first steps you should do before you start to create the presentation layer. When Visual Studio is used, creating a DAL based on a strong DataSet is a task that can be completed within 10 to 15 minutes without writing a line of encoding. The subsequent tutorials will be based on this DAL. In the next tutorial, we will define a bunch of business rules and then see how to implement these rules in a separate business logic layer.
- How to deploy the asp.net mvc program in IIS6.0
- Use Winform to build the asp.net mvc Framework
- Programming idea of ASP. NET Session failure
- ASP. NET Session state storage
- Understand ASP. NET Web Application Models