Building an asp.net shopping CART Using DataTables

xiaoxiao2021-03-06  112

Most Dynamic Web Applications Are Created for the sole purpose of make Money on the web.

Let's face it: why would you go through all the work of creating a dynamic Web application if you do not plan to make money through it Sure, companies employ dynamic intranet sites and there are still some - although very few - free? Web Applications You CAN Use.

In the real world, however, dynamic Web applications are created in an attempt to make money by allowing site owners to sell merchandise on the Web. Providing users with the capability to add items to a virtual shopping cart as they browse through your Website is still very much a business that commands good money. Companies such as VeriSign, Paypal, WebAssist, and LinkPoint charge to provide developers with the capability to add shopping cart functionality to their own Websites. But why pay $ 300- $ 400 for a solution that someone else already BUILT, WHEN YOU BUILD One Just As Easily YourSelf Utilizing New Technology In Asp.net, for Free?

This Article Will Allow You to Develop and Implement Your Own Shopping Cart Utilizing A Session, A DataGrid, and The DataTable Class of The DataSet Object. Through this article, you'll Learn TO:

Build a user interface using ASP.NET Web Server Controls Dynamically construct a DataTable depending on user interaction Bind the dynamically constructed DataTable to a DataGrid Allow the user to remove items from the cart freely Keep a running cost total of items within the cart

At the end of this article, you'll have a fully functioning shopping cart, and you'll have gained a thorough understanding of DataTables, DataGrids, and Session Variables. Download a complete demonstration of the final product here.

This Project is Separated Into Five Parts: building the user interface building the dataatable structure add items to the cart keeping a Running Total Removing Items from the Cart

Step 1: building the shoping CART User Interface

The User Interface Or Ui for the Application IS QUITE SIMPLE. IN FACT, MOST OF THE INTERACTION WILL HTHIN THE DATAGRID, But The Ui Does Include Some Key Components That The User Will BE Intecting with:

A DropDownList Web control that will display the products that we'll offer. The cost of each item will be associated with the value of the DropDownList Web control for simplicity's sake. A TextBox Web control that offers the user the ability to adjust quantities A Button Web Control To Add to the Cart A DataGrid That Will Contain The Cart's Contents a Label Control That Will Display To The User A Running Total in Terms of Price

. Now that you have an idea of ​​what the UI will display, let's add these components to the body of an HTML page Using Dreamweaver MX, we'll create a new page and add this code into the tag of the page:

Product:
socks PANTS )> "" 12.99> HAT
Quantity:






Total: The code is actually quite simple and needs very little explanation. Basically, a hard-coded DropDownList control (ddlProducts) is added, with four products. The cost of those products is maintained by associating a Decimal Value for Each Specific Product.

Second, a TextBox control (txtQuantity) is added so that the user can modify quantities. Next, a Button control (btnAdd) is added with the text "Add to Cart". The onClick event associated with the Button control will call the AddToCart ( ) Subroutine, Which Will Be CREATED in the next section.

Next, a DataGrid (dg) is added which will be used to bind to the dynamically constructed DataTable. Remember, the DataTable will be constructed in code, and bound to the DataGrid for presentation to the user. We'll add a button column to allow the user to remove a specific item if he or she wishes a little later. Finally, we'll add a Label control (lblTotal), which will be used to display to the user a running total of the items within the cart.Step 2: Building The DataTable Structure

If you're familiar with DataSets, then you know that DataTables provide you with a way to dynamically create a purely memory-resident representation of a database table. Typically, you'd fill a DataTable from an existing database, but you could also create One Programmatically, As Will Be The Case Here.

In a DataTable, columns are represented by the columns property, and rows are represented by the rows property. Thus, DataTables will be the perfect choice for the creation of our shopping cart. We can build the columns just as we would within a database, using the columns property of the DataTable, and add rows to the DataTable with the rows property. with the DataTable built, we can then bind the DataTable to a DataGrid to display the results in an intuitive manner.

Because DataTables contain rows and columns, you will be able to effectively mock the structure of a conventional database table. The rows will be added to the DataTable as the user adds items to the cart. For now, we'll need to construct the columns That Will Serve As The Categories for the Row Items. in Order for the Cart To Function Correctly, We'll Need To Add The Following Columns with a Corresponding Data Type:

You're probably wondering how data types, auto increment, and uniqueness will be set programmatically. Remember, DataTables contain column and row properties. Some of those properties include the ability to set the above mentioned items, just as you would a traditional database table . You'll also notice that the DataTable contains a column for ID. Technically, this column has nothing to do with the shopping cart, but it will have a lot to do with keeping the items in the cart unique, and will allow us to establish a primary key if we ever want to create a relationship with another DataTable.For now we just want the structure of the DataTable built when the page loads for the first time. We do not want to actually start to define rows until the user Selects an item to add to the cart.

To Begin Building The Cart's Structure, Add This Code Into The Head of Your Page: