Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

ASP .NET Web Developer s Guide - Mesbah Ahmed, Chris Garrett

.pdf
Скачиваний:
37
Добавлен:
24.05.2014
Размер:
7.32 Mб
Скачать

500 Chapter 11 • Creating an XML.NET Guestbook

Frequently Asked Questions

The following Frequently Asked Questions, answered by the authors of this book, are designed to both measure your understanding of the concepts presented in this chapter and to assist you with real-life implementation of these concepts. To have your questions about this chapter answered by the author, browse to www.syngress.com/solutions and click on the “Ask the Author” form.

Q: Why does the add.aspx code need the inline XML schema?

A:Add.aspx uses the schema to retrieve the way it needs to write the data to the XML file in the proper order. Say that instead of name before e-mail, you had e-mail before name; add.aspx would write the row with the e-mail field first instead of the name field.

Q: Why won’t the simple guestbook show?

A:.NET expects www.w3.org/1999/XSL/Transform as the XSLT namespace. This does limit you a bit, since the Working Draft version is extremely better than the 1999 version.

Q:I get an error that says,“compilation error, (addClick or Page_Load) is not part of asp:(add.aspx or viewbook.aspx)”.What does that mean?

A:Unfortunately, some of the error handling for ASP.NET still needs tweaking; this is a perfect example.When running the aspx page, it will spit out errors when it finds them within the asp objects, but is not very good at reporting errors within the subs located within the <head> tag.When you see these errors, check the code and try again.

www.syngress.com

Chapter 12

Creating an

ADO.NET

Shopping Cart

Solutions in this chapter:

Setting Up the Database

Creating the Web Services

Using WSDL Web References

Building the Site

Site Administration

Customer Administration

Creating an ADOCatalog

Building an XMLCart

Creating the User Interface

;Summary

;Solutions Fast Track

;Frequently Asked Questions

501

502 Chapter 12 • Creating an ADO.NET Shopping Cart

Introduction

Now that we’ve gotten XML under our belt, let’s start working with ADO.NET. A good way to really see what ADO can do is within the frame of a shopping cart application. In this chapter, we will create a shopping cart application for a fictitious online bookseller called “Book Shop.”

To enable online shoppers to purchase books from our site, our shopping cart application must be able to: authenticate users, show current contents of the cart, and enable add, update, and checkout operations.

We will also need to create a catalog that our shoppers can browse through to add items to their cart. Users should also be able to query books by category and view a range of books at a time. In order to achieve these goals, we will create the following:

A database to store all book details

Stored procedures (MS SQL 2000) or parameterized queries (MS Access 2000) for all add, update, delete, and retrieve operations

Web Services that will handle all database interactions

Web Services Description Language (WSDL) Web references to our Web Services

Server-side classes that will connect the Web Services with our user interface (UI)

Web interface for displaying both our catalog and cart

We will also need to create admin interfaces to handle add, update, delete, and retrieve operations for our customers (site users) and site administrators.The interface that will be created in our example can be seen in Figure 12.1.

Setting Up the Database

First, we will design the database for our shopping cart.We will start out by designing an MS Access 2000 database which we will then upsize to a SQL Server 2000 database.

We are creating what is called a relational database. A relational database is a series of tables that represent entities related to one another. Let’s look at a simple example to help illustrate this point: our database. See Figure 12.2.

www.syngress.com

Creating an ADO.NET Shopping Cart • Chapter 12

503

Figure 12.1 The “Book Shop” User Interface

Figure 12.2 Table Relationship

NOTE

To set up the database in this example, you will need to know some basic fundamentals of database design. A good source is Syngress Publishing’s

Designing SQL Server 2000 Databases for .NET Enterprise Servers.

www.syngress.com

504 Chapter 12 • Creating an ADO.NET Shopping Cart

Table “Books” is an entity that represents all the attributes of a book.Table “Categories” is an entity that represents all the attributes for a specific category. A relationship between the two tables is created by the use of primary and foreign keys.Table “Categories” has an attribute named CAT_ID, which is the primary key for the table.This means simply that CAT_ID uniquely identifies every row in the table.This will ensure we won’t get duplicate rows of data.The same concept is true for the table “Books.”We can create the relationship between the two tables by putting the attribute CAT_ID into the table “Books.” By doing so, we have created a foreign key in the table “Books” which references the table “Categories.”We have now created a one-to-many relationship between the table “Books” and the table “Categories.”

There are three different types of table relationships:

One-to-one Exactly one row corresponds with a matching row of the related table.

One-to-many One row corresponds to many rows of the related table.

Many-to-many Many rows correspond to many rows of the related table.

WARNING

A many-to-many relationship between tables is not a recommended practice. When this type of relationship is created in the design of your database, use a splitter table in-between the two tables that have the affected relationship. This will create two one-to-many relationships and ensure data integrity for your database.

We will now create the entities for our shopping cart application. Entities enable us to map the real world. Since we are making a shopping cart, we need some basic objects to start off with. First of all, we need product.We have chosen to use “Books” as the product for the shopping cart but this could be anything. Next, we need an object that will be using the shopping cart,“Customers.” As in the previous paragraph, we have more than one category of product, or in our case “Books,” so we have another object to map which is “Categories.”The last piece to finish off the whole design is a way to track what is bought, “BookOrders.” Now we need to go over each entity to explain why we have selected the attributes included in each.

www.syngress.com

Creating an ADO.NET Shopping Cart • Chapter 12

505

Setting Up the Table “Books”

The Books table will contain the following attributes:

BK_ISBN This will also be our Primary key for the table since an ISBN is already a global unique identifier.

BK_Author This contains the author’s full name.

BK_Price The price of the book.

BK_Title The book title.

BK_Description A brief description of the book.

BK_ImagePath The path to where we will store the image.

CAT_ID Our foreign key attribute to table “Categories.”

Setting Up the Table “Categories”

The Categories table will contain the following attributes:

CAT_ID The primary key for the table which will be an auto generated number; I will cover this in the next two sections.

CAT_Name The name of the category.

Setting Up the Table “Customer”

The Customer table will contain the following attributes:

CT_ID The primary key for the table, an auto generated number.

CT_FirstName Customer first name.

CT_LastName Customer last name.

CT_Email Customer e-mail.

CT_Password Customer password.

Setting Up the Table “Orders”

The Orders table will contain the following attributes:

OR_ID The primary key for the table, an auto generated number.

CT_ID This is our foreign key attribute to table “Customers.”

www.syngress.com

506Chapter 12 • Creating an ADO.NET Shopping Cart

OR_Date The date of the order.

OR_ShippedDate The date the order ships.

Setting Up the Table “BookOrders”

The BookOrders table is the split table for the handling of our relationship between the tables “Books” and “Orders.”This table includes the following attributes:

OR_ID This is our foreign key attribute to table “Orders.”This is also part of the composite Primary key for the table.

BK_ISBN This is our foreign key attribute to table “Books.”This is the other part of the composite primary key.

BKOR_Quantity The total of number of books.

BKOR_Price The total amount of the order.

Now, lets implement this database in Microsoft Access.

NOTE

It is good practice to come up with a naming convention for your database. The naming convention can be anything of your choosing, just make sure you’re consistent throughout your database. A naming convention is a uniformed way to document your code. In our example, OR_denotes the table “Orders.”

Creating an Access Database

To create a database in Microsoft Access, simply navigate to your program files and select the Access icon.The main window will pull up, prompting you to either pick a database from the list of current databases, create a blank database, or use the wizard. See Figure 12.3.

We want to select the Blank Database option and not the wizard. Select OK, then give the database the name shopDb. Next, select the Tables object. From here, we choose the option Create table in design view.We can now transfer the attributes for the tables into the interface (see Figure 12.4).

www.syngress.com

Creating an ADO.NET Shopping Cart • Chapter 12

507

Figure 12.3 Setting Up the Access Database

Figure 12.4 Creating Tables in Design View

Now we can transfer almost everything that’s been done into the interface. One thing we have not discussed is datatypes.

The following is a list of datatypes we will implement in the database:

Text Text or combinations of text and numbers: maximum size 255 characters.

Currency Used for monetary functions, prevents rounding off of total: size 8 bytes.

www.syngress.com

508Chapter 12 • Creating an ADO.NET Shopping Cart

AutoNumber Unique number automatically inserted when a record is added: size 4 bytes.

Number Numeric data to be used for mathematical calculations: size 1, 2, 4, or 8 bytes.

Date/Time Stores date/time: size 8 bytes.

Yes/No Boolean value, 0 or 1: size 1bit.

Memo Used for storing large amounts of text: maximum size 64,000 characters.

OLE Object Can store Word docs, Excel files, and so on: maximum size 1 gigabyte.

Continue this process for the rest of the tables. If you want, you can load the shopDb.mdb from the CD that accompanies this book, then view the complete database. Let’s look at the complete diagram generated by Access after we finish filling in our tables (shown in Figure 12.5).

Figure 12.5 A Database Diagram

To generate the preceding diagram, go to the Tools menu and select the Relationships option.You will be prompted for what tables to add. Select the tables you have created and hit OK.To create the relationships between the tables, left-click the attribute you want to make a relationship with and drag it over to the table that has the matching attribute, release the mouse and you will be prompted with a set of options for the relationship. See Figure 12.6.

www.syngress.com

Creating an ADO.NET Shopping Cart • Chapter 12

509

Figure 12.6 Defining Relationships in Access

The default is to have the Enforce Referential Integrity option selected.This is good enough for our example; the other two options will enable cascading deletes and updates.

WARNING

When defining relationships, make sure the column is of the same datatype as the one you are trying to make a relationship with, otherwise Access will throw an error.

We will do what is called de-normalize the database for the Access version to make things flow between the Data tier of our application and the two different databases. Since our shopping cart uses all OleDb connections to the database regardless of source, the stored procedures created in the SQL Db are the same for the Access version, but we have some limitations when it comes to Access.We cannot easily return the submitted record ID from the table like we can in SQL using the global variable @@identity, so we must solve this by eliminating the Orders table in the schema for Access and adding those rows to the BookOrders table.This will result in customers having multiple order entries, but keeps all data handling code the same for both databases. If you were to program this application, you would select one or the other and optimize accordingly—we are going to straddle the fence here and show both in the same logic.

Now that we have our database schema done, we can upsize the database using the Access Upsizing Wizard and make a SQL server version. Go to Tools, select Database Utilities, then select Upsizing Wizard. Follow the wizard and choose all the defaults.

www.syngress.com