Code Samples -- JScript
Example #4 - Update
<%@ Language=JScript %> <% var oConn = Server.CreateObject("OpenX2.Connection") var oCommand = Server.CreateObject("OpenX2.Command") var sResult = ""; var sResult1 = ""; var bError = false; var bMakeUpdate = Request.Form("id").Count > 0; var sRetrieveSQL = "SELECT * FROM authors"; var sUpdateSQL = "UPDATE authors SET au_lname = :1, au_fname = :2, phone = :3, address = :4, city = :5, state = :6, zip = :7, contract = :8 WHERE authors.au_id = :id"; try { var i; oConn.Connect("ms_ox1"); oCommand.Connection = oConn; oConn.autoCommit = true; if(bMakeUpdate) { oCommand.SetCommandText(sUpdateSQL, 1); for(i = 1; i <= 8; i++) oCommand.ParamValueAsString(i) = new String(Request.Form("fld" + i)); oCommand.ParamValueAsString("id") = new String(Request.Form("id")); oCommand.Execute(); } oCommand.CommandText = sRetrieveSQL; oCommand.Execute(); sResult += "<table border=1><tr>"; for(i = 1; i <= oCommand.FieldCount; i++) { sResult += "<td><b>" + oCommand.FieldName(i) + "</b></td>"; } sResult += "</tr>"; while(oCommand.MoveNext()) { sResult += "<form action=\"OX2test5.asp\" method=\"post\">"; sResult += "<tr>"; sResult += "<td><input type=\"submit\" value=\"Update Row\"><input type=\"hidden\" name=\"id\" value=\"" + oCommand.FieldValueAsString(1) + "\"></td>"; for(i = 2; i <= oCommand.FieldCount; i++) { sResult += "<td><input type=\"text\" name=\"fld" + (i - 1) + "\" value=\"" + oCommand.FieldValueAsString(i) + "\" size=\"" + (oCommand.FieldSize(i) > 20 ? 20 : oCommand.FieldSize(i)) + "\"></td>"; } sResult += "</tr></form>"; } sResult += "</table>"; } 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 #5 - UPDATE Sample</title></head> <body> <%= sResult1 %> <div><%=sResult%></div> </body> </html> |