<script language='JavaScript' runat='server'> // ~~~ The definition of the create method of our ticket object starts here ~~~ // this function was designed to add a row to a table name 'tblTickets' in a SQL Server // database with the fields userName varChar(100), description varChar(1000) and // severity int and a System DSN named 'helpdesk'. // ~~~ The definition of the create method of our ticket object starts here ~~~ // this function was designed to add a row to a table name 'tblTickets' in a SQL Server // database with the fields userName varChar(100), description varChar(1000) and // severity int and a System DSN named 'helpdesk'. function createTicket() { var objRegExp = new RegExp("'", "gi"); // these two lines take care of escaping var strReplaceText = "''"; // single quotes in the SQL statement // the lines below reference 'this.' and then a property name // these are the properties we will add to our object below var strUser = this.user; var strDescription = this.description; var intSeverity = this.severity; // this code inserts a new row into the MS SQL Server // table 'tblTickets' and returns the @@ERROR code (0 if successful) var strCommandText = "INSERT INTO tblTickets (userName, description, severity)\n"; strCommandText += "VALUES ('" + strUser.replace(objRegExp, strReplaceText); strCommandText += "', '" + strDescription.replace(objRegExp, strReplaceText) strCommandText += "', " + intSeverity + ")\n"; strCommandText += "SELECT @@IDENTITY AS tkt_id" // here we escape any single quotes or SQL server will complain: strCommandText = strCommandText; var objConn = Server.CreateObject("ADODB.Connection"); // open our ADO connection and Execute the SQL command objConn.Open("dsn=helpdesk", "sa", ""); objConn.Execute(strCommandText); // Next return the error code, if any, to a variable. Notice how you can treat the // connection object's Execute result as an implicit recordset object, using the GetString() // method on the same line as the Execute method. var returnErrCode = objConn.Execute("SELECT @@ERROR as errorCode").GetString(); objConn.Close(); objConn = null; // Return the error code return(returnErrCode); } // The cTicket function is the object constructor // which defines our JavaScript object function cTicket() { this.user = new String(); this.description = new String(); this.severity = new Number(); this.create = createTicket; // This assigns the function to the create method } function Ticket() { // Creates an instance of the object defined by cTicket var obj = new cTicket(); // And returns it to the function (which is available to VBScript) return(obj); obj = null; } </script>