p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

proasp_codeclinic thread: Transacted ASP/JavaScript page looses variable value


Message #1 by "Michael Loftus" <michael@l...> on Mon, 25 Sep 2000 22:07:01 +0100
I have an ASP form submitting values to a transacted page of JavaScript
code which calls a SQL stored proc and inserts order values into the
database.  The page is a slightly modified version of the Wrox
Professional JavaScript ProcessOrder.asp page on p. 508 of that book.  The
sproc returns an order ID, which is used later in the code successfully. 
This is all done in a JavaScript Try/Catch statement.  Then, when the
ObjectContext.SetComplete(); calls the OnTransactionCommit() function, the
OrderID value is lost... often, but only sometimes.  This page runs great
on NT4/IIS4 with MTS, but is VERY inconsistent on Win2k/IIS5.  Anyone seen
anything like this before?

Here's the code....


<%@ TRANSACTION=Requires_New Language="JScript" %>
<!--#include file="includes/ConnectionJScript.asi"-->
<%
	Response.Buffer = true;
	var iErrorNo = 0;
	var sErrorDescription;
	// Index of first Item element in form
	var iItemElementStart = 1;

	// ADO constants - makes code more readable
	var adCmdStoredProc = 4;
	var adParamReturnValue = 4;
	var adParamInput = 1;
	var adParamOutput = 2;
	var adVarChar = 200;
	var adInteger = 3;
	var adBoolean = 11;
	var adChar = 129;
	var adSingle = 4;
	
	
function OnTransactionAbort()
{
	// If error is not insufficent stock
	//if (iErrorNo != 547)
	Response.Cookies("Basket")= "";
	Response.Redirect("emailAdminError.asp?E=" + escape(sErrorDescription) +
"&C=" + escape(Request.Cookies("Yourwall")("Email")));
	//Response.Write("OnTransactionAbort() " + sErrorDescription)
}

function OnTransactionCommit()
{
	// Everything went ok - re-direct to confirmation page
	Response.Cookies("Basket")= "";
	Response.Redirect("emailAdmin.asp?ID=" + iOrderId + "&p=" +
Request.Form("CCPoints"));
	//Response.Write("Order accepted, OrderID=" + iOrderId);
}
// Process order form
try{
	var loCommand = Server.CreateObject("ADODB.Command");
	var loParam;
	var iCustId = Request.Form("CustomerID");
	var iOrderId = -1;
	var iPhoneOrder = Request.Form("PhoneOrder");
	if (iPhoneOrder == "credit")
		iPhoneOrder = 0;
	else
		iPhoneOrder = 1;
		
// Create ADO command object which will execute our stored procedure
    loCommand.CommandText = "sp_NewOrder";
    loCommand.CommandType = adCmdStoredProc;
    loCommand.Name = "NewOrder";

	loParam = loCommand.CreateParameter("RV", adInteger, adParamReturnValue,
0);
	loCommand.Parameters.Append(loParam);
	loParam = loCommand.CreateParameter("@CustomerID", adInteger,
adParamInput, 0, iCustId);
	loCommand.Parameters.Append(loParam);
	loParam = loCommand.CreateParameter("@OrderDate", adVarChar,
adParamInput, 30, Date());
	loCommand.Parameters.Append(loParam);
	loParam = loCommand.CreateParameter("@FirstNameBill", adVarChar,
adParamInput, 75, Request.Form("FirstNameBill"));
	loCommand.Parameters.Append(loParam);
	loParam = loCommand.CreateParameter("@FirstNameShip", adVarChar,
adParamInput, 75, Request.Form("FirstNameShip"));
	loCommand.Parameters.Append(loParam);
	loParam = loCommand.CreateParameter("@LastNameBill", adVarChar,
adParamInput, 75, Request.Form("LastNameBill"));
	loCommand.Parameters.Append(loParam);
	loParam = loCommand.CreateParameter("@LastNameShip", adVarChar,
adParamInput, 75, Request.Form("LastNameShip"));
	loCommand.Parameters.Append(loParam);
	loParam = loCommand.CreateParameter("@Address1Bill", adVarChar,
adParamInput, 150, Request.Form("Address1Bill"));
	loCommand.Parameters.Append(loParam);
	loParam = loCommand.CreateParameter("@Address1Ship", adVarChar,
adParamInput, 150, Request.Form("Address1Ship"));
	loCommand.Parameters.Append(loParam);
	loParam = loCommand.CreateParameter("@Address2Bill", adVarChar,
adParamInput, 150, Request.Form("Address2Bill"));
	loCommand.Parameters.Append(loParam);
	loParam = loCommand.CreateParameter("@Address2Ship", adVarChar,
adParamInput, 150, Request.Form("Address2Ship"));
	loCommand.Parameters.Append(loParam);
	loParam = loCommand.CreateParameter("@CityBill", adVarChar, adParamInput,
75, Request.Form("CityBill"));
	loCommand.Parameters.Append(loParam);
	loParam = loCommand.CreateParameter("@CityShip", adVarChar, adParamInput,
75, Request.Form("CityShip"));
	loCommand.Parameters.Append(loParam);
	loParam = loCommand.CreateParameter("@StateBill", adVarChar,
adParamInput, 50, Request.Form("StateBill"));
	loCommand.Parameters.Append(loParam);
	loParam = loCommand.CreateParameter("@StateShip", adVarChar,
adParamInput, 50, Request.Form("StateShip"));
	loCommand.Parameters.Append(loParam);
	loParam = loCommand.CreateParameter("@ZipBill", adChar, adParamInput, 10,
Request.Form("ZipBill"));
	loCommand.Parameters.Append(loParam);
	loParam = loCommand.CreateParameter("@ZipShip", adChar, adParamInput, 10,
Request.Form("ZipShip"));
	loCommand.Parameters.Append(loParam);
	loParam = loCommand.CreateParameter("@CountryBill", adVarChar,
adParamInput, 75, Request.Form("CountryBill"));
	loCommand.Parameters.Append(loParam);
	loParam = loCommand.CreateParameter("@CountryShip", adVarChar,
adParamInput, 75, Request.Form("CountryShip"));
	loCommand.Parameters.Append(loParam);
	loParam = loCommand.CreateParameter("@PhoneBill", adChar, adParamInput,
14, Request.Form("PhoneBill"));
	loCommand.Parameters.Append(loParam);
	loParam = loCommand.CreateParameter("@PhoneShip", adChar, adParamInput,
14, Request.Form("PhoneShip"));
	loCommand.Parameters.Append(loParam);
	loParam = loCommand.CreateParameter("@CardHolderName", adVarChar,
adParamInput, 150, Request.Form("cardholdername"));
	loCommand.Parameters.Append(loParam);
	loParam = loCommand.CreateParameter("@CCardNum", adVarChar, adParamInput,
30, Request.Form("cardnumber"));
	loCommand.Parameters.Append(loParam);
	loParam = loCommand.CreateParameter("@CCardExpiration", adVarChar,
adParamInput, 30, Request.Form("expiration"));
	loCommand.Parameters.Append(loParam);
	loParam = loCommand.CreateParameter("@PhoneOrder", adBoolean,
adParamInput, 0, iPhoneOrder);
	loCommand.Parameters.Append(loParam);
	loParam = loCommand.CreateParameter("@SubTotal", adSingle, adParamInput,
0, Request.Form("SubTotal"));
	loCommand.Parameters.Append(loParam);
	loParam = loCommand.CreateParameter("@Shipping", adSingle, adParamInput,
0, Request.Form("Shipping"));
	loCommand.Parameters.Append(loParam);
	loParam = loCommand.CreateParameter("@Tax", adSingle, adParamInput, 0,
Request.Form("Tax"));
	loCommand.Parameters.Append(loParam);
	loParam = loCommand.CreateParameter("@Total", adSingle, adParamInput, 0,
Request.Form("Total"));
	loCommand.Parameters.Append(loParam);
	loParam = loCommand.CreateParameter("@CCPoints", adInteger, adParamInput,
0, Request.Form("CCPoints"));
	loCommand.Parameters.Append(loParam);
	loParam = loCommand.CreateParameter("@Status", adVarChar, adParamInput,
50, "New Order");
	loCommand.Parameters.Append(loParam);
	loParam = loCommand.CreateParameter("@TransactionID", adInteger,
adParamOutput, 0);
	loCommand.Parameters.Append(loParam);
	

	loCommand.ActiveConnection = Application("conn");
	
	loCommand.Execute();
	// check if stored procedure executed ok
	// abort transaction if failed
	
	if (loCommand.Parameters("RV") != 0)
	{
		// get Stored Procs return value
		iErrorNo = loCommand.Parameters("RV");
		ObjectContext.SetAbort();
	}
	else
	{
		// Retrieve OrderId from
		// stored procs output variables
		iOrderId = loCommand.Parameters("@TransactionID");
		
		// Create new command object
		// to add each order item detail to database
		loCommand = null;
		loCommand = Server.CreateObject("ADODB.Command");
		
		loCommand.CommandText = "sp_addOrderItem";
	   	loCommand.CommandType = adCmdStoredProc;
	   	loCommand.Name = "AddOrderItem";
		
		loParam = loCommand.CreateParameter("RV", adInteger, adParamReturnValue,
0);
		loCommand.Parameters.Append(loParam);
		loParam = loCommand.CreateParameter("@TransactionID", adInteger,
adParamInput, 0, iOrderId);
		loCommand.Parameters.Append(loParam);
		loParam = loCommand.CreateParameter("@PhotoID", adInteger, adParamInput,
0);
		loCommand.Parameters.Append(loParam);
		loParam = loCommand.CreateParameter("@Quantity", adInteger,
adParamInput, 0);
		loCommand.Parameters.Append(loParam);
		
	   	
		loCommand.ActiveConnection = Application("conn");
		var sElementKey;
		var iItemId;
		var iQty;
		// Loop through the item form elements
		for (var iElementCounter = iItemElementStart;iElementCounter
<= Request.Form.Count; iElementCounter++)
		{
			sElementKey = new String(Request.Form.Key(iElementCounter));
			// if element name starts with ID its an item form element
			if (sElementKey.substr(3,2) == "ID")
			{
				// Get ItemId from form value passed
				iItemId = parseInt(sElementKey.substring(5,sElementKey.length));
				// move to next element which is quantity value for that item
				iElementCounter++;
				
				// get quantity
				iQty = parseInt(Request.Form(iElementCounter));
				// set command's parameters
				loCommand.Parameters("@PhotoID") = iItemId;
				loCommand.Parameters("@Quantity") = iQty;
				// execute stored procedure
				loCommand.Execute();
				// Any errors?
				if (loCommand.Parameters("RV") != 0)
				{
					// set iErrorNo, abort transaction and break out
					// of loop
					iErrorNo = loCommand.Parameters("RV");
					ObjectContext.SetAbort();
					break;			
				}			
			}
		}
	}

	// only set complete if transaction complete with no errors
	if (iErrorNo == 0)
		ObjectContext.SetComplete();
}
catch(e)
{
	sErrorDescription = e;
	ObjectContext.SetAbort();
}

loCommand = null;
	
%>

  Return to Index