ASP. NET 2.0 data Tutorial: add custom encoding to DAL

Source: Internet
Author: User

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 #

 
 
  1. using System;  
  2. using System.Data;  
  3. using NorthwindTableAdapters;  
  4.  
  5. public partial class   
  6.  
  7. Northwind  
  8. {  
  9.     public partial class   
  10.  
  11. SuppliersRow  
  12.     {  
  13.         public Northwind.ProductsDataTable GetProducts()  
  14.         {  
  15.             ProductsTableAdapter productsAdapter =  
  16.              new ProductsTableAdapter();  
  17.             return 
  18.               productsAdapter.GetProductsBySupplierID(this.SupplierID);  
  19.         }  
  20.     }  
  21. }  
  22.  

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 #

 
 
  1. NorthwindTableAdapters.SuppliersTableAdapter   
  2.  
  3. suppliersAdapter = new   
  4.  
  5. NorthwindTableAdapters.SuppliersTableAdapter();  
  6.  
  7. // Get all of the suppliers  
  8. Northwind.SuppliersDataTable suppliers =  
  9.   suppliersAdapter.GetSuppliers();  
  10.  
  11. // Enumerate the suppliers  
  12. foreach (Northwind.SuppliersRow supplier in suppliers)  
  13. {  
  14.     Response.Write("Supplier: " +   
  15.  
  16. supplier.CompanyName);  
  17.     Response.Write("");  
  18.  
  19.     // List the products for this supplier  
  20.     Northwind.ProductsDataTable products = supplier.GetProducts();  
  21.     foreach (Northwind.ProductsRow product in products)  
  22.         Response.Write("" +   
  23.  
  24. product.ProductName + "");  
  25.  
  26.     Response.Write(" ");  
  27. }  
  28.  

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

 
 
  1. < %@ Page Language="C#"   
  2.  
  3. AutoEventWireup="true" CodeFile="SuppliersAndProducts.aspx.cs"   
  4.  
  5. Inherits="SuppliersAndProducts" %>  
  6.  
  7. < !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0   
  8.  
  9. Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  10.  
  11. < html xmlns="http://www.w3.org/1999/xhtml" >  
  12. < head runat="server">  
  13.     < title>Untitled Pagetitle>  
  14.     < link href="Styles.css"   
  15.  
  16. rel="stylesheet"   
  17.  
  18. type="text/css"   
  19.  
  20. />  
  21. head>  
  22. < body>  
  23.     < form id="form1" runat="server">  
  24.     < div>  
  25.         < h1>  
  26.             Suppliers and Their Productsh1>  
  27.         < p>  
  28.             < asp:GridView ID="GridView1" runat="server" 
  29.              AutoGenerateColumns="False" 
  30.              CssClass="DataWebControlStyle">  
  31.                 < HeaderStyle CssClass="HeaderStyle" />  
  32.                 < AlternatingRowStyle CssClass="AlternatingRowStyle" />  
  33.                 < Columns>  
  34.                     < asp:BoundField DataField="CompanyName" 
  35.                       HeaderText="Supplier" />  
  36.                     < asp:TemplateField HeaderText="Products">  
  37.                         < ItemTemplate>  
  38.                             < asp:BulletedList ID="BulletedList1" 
  39.                              runat="server" DataSource="< %#  
  40.               ((Northwind.SuppliersRow)((System.Data.DataRowView)  
  41.               Container.DataItem).Row).GetProducts() %>"  
  42.                                     DataTextField="ProductName">  
  43.                             asp:BulletedList>  
  44.                         ItemTemplate>  
  45.                     asp:TemplateField>  
  46.                 Columns>  
  47.             asp:GridView>  
  48.              p>  
  49.  
  50.     div>  
  51.     form>  
  52. body>  
  53. html>  

SuppliersAndProducts. aspx. cs

C #

 
 
  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Collections;  
  5. using System.Web;  
  6. using System.Web.Security;  
  7. using System.Web.UI;  
  8. using System.Web.UI.WebControls;  
  9. using System.Web.UI.WebControls.WebParts;  
  10. using System.Web.UI.HtmlControls;  
  11. using NorthwindTableAdapters;  
  12.  
  13. public partial class   
  14.  
  15. SuppliersAndProducts : System.Web.UI.Page  
  16. {  
  17.     protected void   
  18.  
  19. Page_Load(object sender, EventArgs e)  
  20.     {  
  21.         SuppliersTableAdapter suppliersAdapter = new 
  22.           SuppliersTableAdapter();  
  23.         GridView1.DataSource = suppliersAdapter.GetSuppliers();  
  24.         GridView1.DataBind();  
  25.     }  
  26. }  
  27.  

 

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.

  1. How to deploy the asp.net mvc program in IIS6.0
  2. Use Winform to build the asp.net mvc Framework
  3. Programming idea of ASP. NET Session failure
  4. ASP. NET Session state storage
  5. Understand ASP. NET Web Application Models

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.