More Detailed Information About Each Product
Introduction. Now
we need to develop an ASP that focuses on using a dynamic publishing
approach to display more detailed information about each product.
The page is called Product.asp.
This page requires two stored procedures.
The following table displays the names of the stored procedures and the ASP file in which they are called. |
Stored Procedure | ASP Container |
sp_RetrieveProduct
sp_Attributes |
Product.asp |
Finally, you need to get some images to be used for the
individual product pages,
|
Product Images | |
Regular Size | |
Name | Image |
thimble.gif | |
static.gif | |
jazz.gif | |
circle.gif | |
hatsoff.gif | |
lassos.gif | |
DogCat.gif | |
candles.gif | |
hole.gif | |
under.gif |
The Stored Procedures. Now I will list out the two stored procedures that you should create using SQL in almost the same way we created the tables. The first file should be called sp_RetrieveProduct.asp. |
<%@ Language=VBScript %> <% Option Explicit %> <!--#include File="adovbs.inc"--> <% ' Open a connection to our SQL Server database ' We will use the ADO Driver connection Dim connDeptProds, strSQLCreate Set connDeptProds = Server.CreateObject("ADODB.Connection") connDeptProds.ConnectionString="Driver={SQL Server}; Server=cisdev.quinnipiac.edu;" & _ "Database=YourUserName;UID=cis; PWD=csatqu" connDeptProds.Open ' Creating the SQL String to create the stored procedure strSQLCreate = "CREATE PROCEDURE sp_RetrieveProduct @idProduct int AS " & _ "select * from products where idProduct = @idProduct" connDeptProds.execute(strSQLCreate) connDeptProds.Close Set connDeptProds = Nothing Response.Write "<font size = 5>The SQL has executed</font>" %> |
The second file should be called sp_Attributes.asp. |
<%@ Language=VBScript %> <% Option Explicit %> <!--#include File="adovbs.inc"--> <% ' Open a connection to our SQL Server database ' We will use the ADO Driver connection Dim connDeptProds, strSQLCreate Set connDeptProds = Server.CreateObject("ADODB.Connection") connDeptProds.ConnectionString="Driver={SQL Server}; Server=cisdev.quinnipiac.edu;" & _ "Database=YourUserName;UID=cis; PWD=csatqu" connDeptProds.Open ' Creating the SQL String to create the stored procedure strSQLCreate = "CREATE PROCEDURE sp_Attributes @idProduct int AS " & _ "select products.idproduct, attribute.idattribute, attribute.chrattributename, attributecategory.chrcategoryname," & _ "productattribute.idproductattribute from products, productattribute, attribute, attributecategory " & _ " where products.idproduct = @idProduct and productattribute.idproduct = @idProduct and " & _ " productattribute.idattribute = attribute.idattribute and attribute.idattributecategory = attributecategory.idattributecategory " & _ "order by chrcategoryname" connDeptProds.execute(strSQLCreate) connDeptProds.Close Set connDeptProds = Nothing Response.Write "<font size = 5>The SQL has executed</font>" %> |
Like all of the other ASPs based on CREATE SQL commands, these files need to be uploaded to your web and then executed once. After they have been executed, you should get an error if you try to execute them again because the stored procedures should already be there. After these sp_name.asp files have been used they should be removed from your space on the server. |
The Product.asp. The following
Product.asp
will
|
<%@ Language=VBScript %> <HTML> <!-- Product.asp - Displays the product information. --> <!-- #include file="include/header.asp" --> <% ' Create an ADO database connection set dbProduct = server.createobject("adodb.connection") ' Create a record set set rsProduct = server.CreateObject("adodb.recordset") ' Open the connection using our SQL Server DSN-less connection dbProduct.ConnectionString="Driver={SQL Server}; Server=cisdev.quinnipiac.edu;" & _ "Database=WildWillies;UID=cis; PWD=csatqu" dbProduct.Open ' Execute the stored procedure to retrieve the product and pass in the id of the product. sql = "execute sp_RetrieveProduct " & request("idProduct") ' Execute the SQL statement set rsProduct = dbProduct.Execute(sql) ' Retrieve the product data txtDescription = rsProduct("txtDescription") chrProductImage = rsProduct("chrProductImage") chrProductName = rsProduct("chrProductName") intPrice = rsProduct("intPrice") idProduct = rsProduct("idProduct") %> <!-- The additem.asp page will be called to add the product to the basket --> <form method="post" action="additem.asp"> <!-- The table will provide the layout structure for the product. --> <table border="0" cellpadding="3" cellspacing="3"> <!-- Row to display the product image, name and description. --> <TR> <!-- Display the image --> <td><img src="images/products/<%=chrProductImage%>"></td> <!-- Show the product name and description --> <td valign="top"> <CENTER><b><font size="5"><%=chrProductName%></font></b></center> <BR><BR> <%=txtDescription%><BR><BR> </td> </TR> <!-- Show the product price. An input quantity box is created. Also, several hidden variables will hold key data for adding the product to the database. --> <TR> <TD align="center"><B>Price: <%=formatcurrency(intPrice/100, 2)%></b> </td> <TD align="center"> <B>Quantity: <input type="text" value="1" name="quantity" size="2"></b> <input type="hidden" value="<%=idProduct%>" name="idProduct"> <input type="hidden" value="<%=chrProductName%>" name="ProductName"> <input type="hidden" value="<%=intPrice%>" name="ProductPrice"> </td> </TR> <% ' Create an ADO database connection set dbAttributes = server.createobject("adodb.connection") ' Create a record set set rsAttributes = server.CreateObject("adodb.recordset") ' Open the connection using our SQL Server DSN-less connection dbAttributes.ConnectionString="Driver={SQL Server}; Server=cisdev.quinnipiac.edu;" & _ "Database=WildWillies;UID=cis; PWD=csatqu" dbAttributes.Open ' Execute the stored procedure to retrieve the attributes for the products. sql = "execute sp_Attributes " & request("idProduct") ' Execute the SQL statement set rsAttributes = dbProduct.Execute(sql) ' Loop through and display the attributes for the product. if not rsAttributes.EOF then %>
<%
' Check to see if we have moved beyond the color attribute in the list.
' Exit the do loop
' Move to the next row
end if |
Well, how's that for using ASPs and SQL Server to actually do something realistic! Far too often students think that course work is artificially difficult. That is why I try to make sure and tie my coding to realistic situations. The "real world" of coding always requires more than you think it will! |