home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 32 / IOPROG_32.ISO / SOFT / SqlEval7 / devtools / samples / ADO / Web / add.asp next >
Encoding:
Text File  |  1998-10-04  |  8.1 KB  |  263 lines

  1. <%@ LANGUAGE="VBSCRIPT" %>
  2. <% 'ON ERROR RESUME NEXT 'Set this so that errors can be handled %>
  3.  
  4. <HTML>
  5. <HEAD>
  6. <META NAME="GENERATOR" Content="Microsoft Visual InterDev 6.0">
  7. <META HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1">
  8. <TITLE>NIMS - Add a Product</TITLE>
  9. <LINK REL="stylesheet" TYPE="text/css" HREF="nimstyle.css">
  10. <!-- #include file="adovbs.inc" -->
  11. </HEAD>
  12. <SCRIPT LANGUAGE="VBScript">
  13. <!--
  14. 'This clientside VBScript validates the add form to make sure the user 
  15. 'entered valid product information.
  16. DIM ValidProduct
  17.  
  18. Sub ValidateAddForm
  19.  
  20.     SET Add = document.AddForm
  21.     ValidProduct = True
  22.     
  23.     Call CheckField (Add.ProductName.Value, "Please enter a product name!")
  24.     Call CheckField (Add.QuantityPerUnit.Value, "Please enter a quantity per unit!")
  25.  
  26.     Call CheckField (Add.UnitPrice.Value, "Please enter a Unit Price!")
  27.     Call CheckFieldNumeric (Add.UnitPrice.Value, "The Unit Price must be a monetary value!")
  28.     Call CheckMonetaryField 
  29.     
  30.     Call CheckField (Add.UnitsInStock.Value, "Please enter Units In Stock!")
  31.     Call CheckFieldNumeric (Add.UnitsInStock.Value, "Units In Stock must be a numeric value!")
  32.     
  33.     Call CheckField (Add.UnitsOnOrder.Value, "Please enter Units On Order!")
  34.     Call CheckFieldNumeric (Add.UnitsOnOrder.Value, "Units On Order must be a numeric value!")
  35.     
  36.     Call CheckField (Add.ReorderLevel.Value, "Please enter the Reorder Level!")
  37.     Call CheckFieldNumeric (Add.ReorderLevel.Value, "The Reorder Level must be a numeric value!")
  38.     
  39.     IF ValidProduct THEN
  40.         'MsgBox "I'm adding your project now!"
  41.         Add.Submit
  42.     END IF
  43.     
  44. END Sub
  45.  
  46. Sub CheckField (ByVal strFieldValue, ByVal strMsg)
  47.   IF ValidProduct THEN 'Check to see if the field is invalid
  48.     'If a field name was not entered, prompt the user.
  49.     IF strFieldValue = "" THEN
  50.         ValidProduct = False
  51.         MsgBox strMsg
  52.     END IF
  53.   END IF
  54. END Sub 'Check Field
  55.  
  56. Sub CheckFieldNumeric (ByVal strFieldValue, ByVal strMsg)
  57.     IF ValidProduct THEN 'Check to see if the field is invalid with non-numeric values
  58.         IF NOT IsNumeric(strFieldValue) THEN
  59.             ValidProduct = False
  60.             MsgBox strMsg
  61.         ELSE 'It is numeric, make sure it is a positive number
  62.             IF NOT strFieldValue >= 0 THEN 'Set project to not valid
  63.                 ValidProduct = False
  64.                 MsgBox "All numeric values must be greater than or equal to zero!"
  65.             END IF
  66.         END IF
  67.     END IF
  68. END Sub 'Check Field Numeric
  69.  
  70. Sub CheckMonetaryField
  71.     IF ValidProduct THEN 'Check to see if the Unit Price field value is within
  72.                          'the proper range
  73.         IF AddForm.UnitPrice.Value > 99999999999999.99 THEN
  74.             ValidProduct = False
  75.             MsgBox "The unit price must be less than $99,999,999,999,999.99!"
  76.         END IF
  77.         
  78.     END IF
  79. END Sub 'Check Monetary Field
  80. -->
  81. </SCRIPT>
  82. <BODY>
  83.  
  84. <% 
  85. 'Start processing form data if any
  86. IF Request.Form("ProductName") <> "" THEN 
  87.  
  88.     'Retrive the CategoryID from the input 
  89.     CategoryID = Request.Form("CategoryID")
  90.     
  91.     'Reference the Session connection variable
  92.     Set cn = Session("cnn")
  93.     
  94.     'Create a recordset
  95.     Set ProductInfo = Server.CreateObject("ADODB.Recordset") 
  96.  
  97.     'Create a query string
  98.     querystr = "SELECT ProductName, SupplierID, CategoryID, QuantityPerUnit, UnitPrice, UnitsInStock," _
  99.             & "UnitsOnOrder, ReorderLevel, Discontinued, ProductID FROM Products" _
  100.             & " WHERE CategoryID = " & CategoryID & " ORDER BY ProductID"
  101.  
  102.     'Set all the properties for the recordset
  103.     ProductInfo.ActiveConnection = cn
  104.     ProductInfo.CursorType = adOpenKeyset
  105.     ProductInfo.LockType = adLockOptimistic
  106.     ProductInfo.Source = querystr
  107.     ProductInfo.Open
  108.  
  109.     'Call the Recordset method AddNew to create a new record in the recordset
  110.     ProductInfo.AddNew
  111.  
  112.     'Set the values of the new record equal to the values entered by the user
  113.     'in the Add Product form.
  114.     ProductInfo("ProductName") = Request.Form("ProductName")
  115.     ProductInfo("SupplierID") = Request.Form("SupplierID")
  116.     ProductInfo("CategoryID") = Request.Form("CategoryID")
  117.     ProductInfo("QuantityPerUnit") = Request.Form("QuantityPerUnit")
  118.     ProductInfo("UnitPrice") = Request.Form("UnitPrice")
  119.     ProductInfo("UnitsInStock") = Request.Form("UnitsInStock")
  120.     ProductInfo("UnitsOnOrder") = Request.Form("UnitsOnOrder")
  121.     ProductInfo("ReorderLevel") = Request.Form("ReorderLevel")
  122.     ProductInfo("Discontinued") = Request.Form("Discontinued")
  123.  
  124.     'Call the Recordset method Update to actually add the new record to the
  125.     'SQL Server database
  126.     ProductInfo.Update %>
  127.     
  128.     <!-- Output the new product entry to the user so they can see what was added. -->
  129.     <H3>The following product information has been added to the inventory database:</H3>
  130.     <HR>
  131.  
  132.     <TABLE CELLPADDING=5>
  133.     <TR>
  134. <%  'Output the Table header row
  135.     For i = 0 To 8
  136.            Response.Write "<TD CLASS=header>" & ProductInfo.Fields(i).Name & "</TD>"
  137.     NEXT %>
  138.    
  139.     </TR>
  140.     <TR>
  141.     
  142. <%    'Output the row containing the added product's information
  143.     For i = 0 to 8
  144.         Response.Write "<TD>"
  145.         Response.Write ProductInfo.Fields(i).Value 
  146.         Response.Write "</TD>"
  147.     NEXT %>
  148.     
  149.     </TR>
  150.     </TABLE>
  151.  
  152.     <BR>
  153.     <HR>
  154.     Return to the <A HREF="default.htm">Main Menu</A>
  155.  
  156. <%    'Close the recordset after use
  157.     ProductInfo.Close
  158.  
  159. ELSE 'Output a form so that the user can add a new cd to the collection %>
  160.  
  161. <CENTER>
  162. <H2>Add a Product to the Inventory</H2>
  163. </CENTER>
  164. <HR>
  165. To add a product to the Northwind Inventory, fill out the following form and then press
  166. the button at the bottom of the page.
  167.  
  168. <FORM NAME="AddForm" ACTION="add.asp" METHOD=POST>
  169. <TABLE ALIGN=CENTER CLASS=formtable CELLPADDING=2>
  170.     <TR>
  171.         <TD>Product Name</TD>
  172.         <TD><INPUT TYPE="text" NAME="ProductName" MAXLENGTH=40></TD>
  173.     </TR>
  174.     <TR>
  175.         <TD>Supplier</TD>
  176.         <TD><SELECT NAME="SupplierID" SIZE=1>
  177.             <OPTION VALUE=18>Aux joyeux eccl├⌐siastiques
  178.             <OPTION VALUE=16>Bigfoot Breweries
  179.             <OPTION VALUE=5>Cooperativa de Quesos 'Las Cabras'
  180.             <OPTION VALUE=27>Escargots Nouveaux
  181.             <OPTION VALUE=1>Exotic Liquids
  182.             <OPTION VALUE=29>For├¬ts d'├⌐rables
  183.             <OPTION VALUE=14>Formaggi Fortini s.r.l.
  184.             <OPTION VALUE=28>Gai p├óturage
  185.             <OPTION VALUE=24>G'day, Mate
  186.             <OPTION VALUE=3>Grandma Kelly's Homestead
  187.             <OPTION VALUE=11>Heli S├╝├ƒwaren GmbH & Co. KG
  188.             <OPTION VALUE=23>Karkki Oy
  189.             <OPTION VALUE=20>Leka Trading
  190.             <OPTION VALUE=21>Lyngbysild
  191.             <OPTION VALUE=25>Ma Maison
  192.             <OPTION VALUE=6>Mayumi's
  193.             <OPTION VALUE=19>New England Seafood Cannery
  194.             <OPTION VALUE=2>New Orleans Cajun Delights
  195.             <OPTION VALUE=13>Nord-Ost-Fisch Handelsgesellschaft mbH
  196.             <OPTION VALUE=15>Norske Meierier
  197.             <OPTION VALUE=26>Pasta Buttini s.r.l.
  198.             <OPTION VALUE=7>Pavlova, Ltd.
  199.             <OPTION VALUE=9>PB Kn├ñckebr├╢d AB
  200.             <OPTION VALUE=12>Plutzer Lebensmittelgro├ƒm├ñrkte AG
  201.             <OPTION VALUE=10>Refrescos Americanas LTDA
  202.             <OPTION VALUE=8>Specialty Biscuits, Ltd.
  203.             <OPTION VALUE=17>Svensk Sj├╢f├╢da AB
  204.             <OPTION VALUE=4>Tokyo Traders
  205.             <OPTION VALUE=22>Zaanse Snoepfabriek
  206.             </SELECT>
  207.         </TD>
  208.     </TR>
  209.     <TR>
  210.         <TD>Category</TD>
  211.         <TD><SELECT NAME="CategoryID" SIZE=1>
  212.             <OPTION VALUE=1>Beverages
  213.             <OPTION VALUE=2>Condiments
  214.             <OPTION VALUE=3>Confections
  215.             <OPTION VALUE=4>Dairy Products
  216.             <OPTION VALUE=5>Grains/Cereals
  217.             <OPTION VALUE=6>Meat/Poultry
  218.             <OPTION VALUE=7>Produce
  219.             <OPTION VALUE=8>Seafood
  220.             </SELECT>
  221.         </TD>
  222.     </TR>
  223.     <TR>
  224.         <TD>Quantity Per Unit</TD>
  225.         <TD><INPUT TYPE="text" NAME="QuantityPerUnit" MAXLENGTH=20></TD>
  226.     </TR>
  227.     <TR>
  228.         <TD>Unit Price</TD>
  229.         <TD><INPUT TYPE="text" NAME="UnitPrice"></TD>
  230.     </TR>
  231.     <TR>
  232.         <TD>Units In Stock</TD>
  233.         <TD><INPUT TYPE="text" NAME="UnitsInStock" SIZE=4 MAXLENGTH=4></TD>
  234.     </TR>
  235.     <TR>
  236.         <TD>Units On Order</TD>
  237.         <TD><INPUT TYPE="text" NAME="UnitsOnOrder" SIZE=4 MAXLENGTH=4></TD>
  238.     </TR>
  239.     <TR>
  240.         <TD>Reorder Level</TD>
  241.         <TD><INPUT TYPE="text" NAME="ReorderLevel" SIZE=4 MAXLENGTH=4></TD>
  242.     </TR>
  243.     <TR>
  244.         <TD>Discontinued</TD>
  245.         <TD><INPUT TYPE="radio" NAME="Discontinued" VALUE=1>True
  246.             <INPUT TYPE="radio" NAME="Discontinued" VALUE=0 CHECKED>False
  247.         </TD>
  248.     </TR>
  249.     <TR>
  250.         <TD></TD>
  251.         <TD><INPUT TYPE="button" NAME="AddButton" 
  252.              VALUE="Add Product To Inventory" onClick="VBScript:ValidateAddForm"></TD>
  253.     </TR>
  254. </TABLE>
  255. </FORM>
  256.  
  257. <BR><BR><HR>
  258. Return to the <A HREF="default.htm">Main Menu</A>
  259. <% END IF %>
  260.  
  261. </BODY>
  262. </HTML>
  263.