Code Samples -- JScript

Example #6 - Insert
<%@ Language=JScript %>
<%
var oConn = Server.CreateObject("OpenX2.Connection")
var oCommand = Server.CreateObject("OpenX2.Command")
var sResult = "";
var bError = false;
var bInsert = (Request.Form("id").Count > 0) && (Request.Form("id") == "Add New");
var sRetrieveSQL = "SELECT * FROM authors";
var sInsertSQL = "INSERT INTO authors ( au_id, au_lname, au_fname, phone, address, city, state, zip, contract ) VALUES ( :1, :2, :3, :4, :5, :6, :7, :8, :9 )";

try {
  var i;
  oConn.Connect("ms_ox1");
  oCommand.Connection = oConn;
  oConn.autoCommit = true;
  if(bInsert)  {
    var rnd = new String(Math.round(Math.random() * 1000000000));
    oCommand.CommandText = sInsertSQL;
    oCommand.ParamValueAsString(1) = rnd.substr(0,3) + "-" + rnd.substr(3,2) + "-" + rnd.substr(5,4);
    for(i = 2; i < 10; i++)
      oCommand.ParamValueAsString(i) = new String(Request.Form("fld" + i));
    oCommand.Execute();
  }
  oCommand.CommandText = sRetrieveSQL;
  oCommand.Execute();
  sResult += "<form action=\"OX2test7.asp\" method=\"post\">";
  sResult += "<table border=1><tr>";
  for(i = 1; i <= oCommand.FieldCount; i++)  {
    sResult += "<td><b>" + oCommand.FieldName(i) + "</b></td>";
  }
  sResult += "</tr><tr><td><input type=\"submit\" name=\"id\" value=\"Add New\"></td>";
  for(i = 2; i <= oCommand.FieldCount; i++)  {
    sResult += "<td><input type=\"text\" name=\"fld" + i + "\" value=\"\" size=\"" + (oCommand.FieldSize(i) > 20 ? 20 : oCommand.FieldSize(i)) + "\"></td>";
  }
  sResult += "</tr>";

  while(oCommand.MoveNext())  {
    sResult += "<tr>";
    for(i = 1; i <= oCommand.FieldCount; i++)  {
      sResult += "<td style=\"font-size: 10pt;\">" + oCommand.FieldValueAsString(i) + " </td>";
    }
    sResult += "</tr>";
  }
  sResult += "</table></form>";
}
catch(e) {
  bError = true;
  if(oCommand.ErrorCode != 0)
    sResult = "OpenX2 Command Error: " + oCommand.ErrorInfo + ". Error #" + oCommand.ErrorCode + " (" + oCommand.ErrorCodeEx + ")<br />";
  else
    if(oConn.ErrorCode != 0)
      sResult += "OpenX2 Connection Error: " + oConn.ErrorInfo + ". Error #" + oConn.ErrorCode + " (" + oConn.ErrorCodeEx + ")<br />";

  sResult += "ASP Error: #" + e.number + ". " + e.description + "<br />";
}
%>

<html>
  <head><title>OpenX2 Test #7 - INSERT Sample</title></head>
  <body>
    <div><%=sResult%></div>
  </body>
</html>