0

I have a large JavaScript function that is having problems. I added a try/catch block hoping it would report errors. However, no errors are reported. The function returns and empty string which means it did not work. It should return the key value of the inserted record. I have attempted to check for errors in the connection object also. An ODBC connection is being used. Below is the code. I understand the script will continue to run even if an ADO error occurs. I am open to ideas.

function addSelectedFile_Aspera(TransmittalKey, FileName, OriginalFilePath, DestinationPath, TransferType, UserKey) {
    try {
        TransmittalKey = unescape(TransmittalKey);
        FileName = unescape(FileName);
        OriginalFilePath = unescape(OriginalFilePath);
        DestinationPath = unescape(DestinationPath);
        TransferType = unescape(TransferType);
        UserKey = unescape(UserKey);

        var DataConn = Server.CreateObject("ADODB.Connection");
        DataConn.ConnectionTimeout = Application("DataConn_ConnectionTimeout");
        DataConn.CommandTimeout = Application("DataConn_CommandTimeout");
        DataConn.Open(Application("DataConn_ConnectionString"), Application("DataConn_RuntimeUserName"), Application("DataConn_RuntimePassword"));
        var RS = Server.CreateObject("ADODB.RecordSet");

        var strSQL = "select OriginalFilePath from bms_FileTransfers where TransmittalKey=" + TransmittalKey;
        strSQL = strSQL + " and OriginalFilePath='" + OriginalFilePath + "' and DateTransferred is null";

        RS.Open(strSQL, DataConn);
        if (!RS.EOF) {
            RS.Close();
            RS = null;
            DataConn.Close();
            DataConn = null;
            return ("Already exists");
        }
        RS.Close();

        var DateTransferred = TodaysDate();
        var TimeTransferred = CurrentTime();

        var cmdTemp = Server.CreateObject("ADODB.Command");
        cmdTemp.ActiveConnection = DataConn;

        var AddedFileKeyValue = generatePrimaryKey("bms_FileTransfers", "FileTransferKey");

        var str = "";
        str = "insert into bms_FileTransfers";
        str = str + " (FileTransferKey,FileName,OriginalFilePath,SavedFilePath";
        str = str + ",DateTransferred,TimeTransferred,TransmittalKey,TransferType)";
        str = str + " values(";
        str = str + AddedFileKeyValue;
        str = str + "," + formatSQLData(FileName, "C");
        str = str + "," + formatSQLData(OriginalFilePath, "C");
        str = str + "," + formatSQLData(DestinationPath, "C");
        str = str + "," + formatSQLData(DateTransferred, "C");
        str = str + "," + formatSQLData(TimeTransferred, "C");
        str = str + "," + TransmittalKey;
        str = str + ",'" + TransferType + "')";

        cmdTemp.CommandText = str;
        cmdTemp.CommandType = 1;
        cmdTemp.Execute();

        // add to audit trail

        var UserName = "";
        var FirstName = "";
        var LastName = "";
        var Middle = "";

        var RS = Server.CreateObject("ADODB.RecordSet");

        strSQL = "select UserName,FirstName,Middle,LastName from bms_SysUsers where UserKey=" + UserKey;

        RS.Open(strSQL, DataConn);
        if (!RS.EOF) {
            UserName = ConvertNullToBlank(RS(0));
            FirstName = ConvertNullToBlank(RS(1));
            Middle = ConvertNullToBlank(RS(2));
            LastName = ConvertNullToBlank(RS(3));
        }
        RS.Close();

        var UserFullName = FirstName + " " + Middle + " " + LastName;
        if (Middle == "") UserFullName = FirstName + " " + LastName;

        var ModuleName = "DataTransmittals";
        var ReasonForChange = "File Uploaded to " + DestinationPath;
        var AuditTrailKey;

        strSQL = "select max(AuditTrailKey) from bms_DataAuditTrail";

        RS.Open(strSQL, DataConn);
        if (!RS.EOF) {
            AuditTrailKey = parseFloat(ConvertNullToZero(RS(0))) + 1;
        }
        RS.Close();

        strSQL = "insert into bms_DataAuditTrail (AuditTrailKey,ModuleName,RecordKeyValue,ColumnName";
        strSQL = strSQL + ",ChangeDate,ChangeTime,ReasonForChange,UserKey,UserID,UserFullName)";
        strSQL = strSQL + " values(";

        strSQL = strSQL + AuditTrailKey;
        strSQL = strSQL + "," + formatSQLData(ModuleName, "C");
        strSQL = strSQL + "," + TransmittalKey;
        strSQL = strSQL + "," + formatSQLData("File Upload", "C");
        strSQL = strSQL + "," + formatSQLData(DateTransferred, "C");
        strSQL = strSQL + "," + formatSQLData(TimeTransferred, "C");
        strSQL = strSQL + "," + formatSQLData(ReasonForChange, "C");
        strSQL = strSQL + "," + UserKey;
        strSQL = strSQL + "," + formatSQLData(UserName, "C");
        strSQL = strSQL + "," + formatSQLData(UserFullName, "C");
        strSQL = strSQL + ")";

        cmdTemp.CommandText = strSQL;
        cmdTemp.CommandType = 1;
        cmdTemp.Execute;

        RS = null;
        cmdTemp = null;
        var myErrorText = "";
        var errLoop = Server.CreateObject("ADODB.Error");
        if (DataConn.Errors.Count > 0) {
            for (var i = 1; i < DataConn.Errors.Count; i++) {
                errLoop = DataConn.Errors(i);
                myErrorText += errLoop.Number;
                myErrorText += errLoop.Description;
                myErrorText += errLoop.Source;
                myErrorText += errLoop.SQLState;
                myErrorText += errLoop.NativeError;
            }
        }
        if (myErrorText != "") {
            var cmdTemp3 = Server.CreateObject("ADODB.Command");
            var DataConn3 = Server.CreateObject("ADODB.Connection");
            DataConn3.ConnectionTimeout = Application("DataConn_ConnectionTimeout");
            DataConn3.CommandTimeout = Application("DataConn_CommandTimeout");
            DataConn3.Open(Application("DataConn_ConnectionString"), Application("DataConn_RuntimeUserName"), Application("DataConn_RuntimePassword"));
            cmdTemp3.ActiveConnection = DataConn3;
            var str3 = "";
            str3 = "insert into bms_ErrorLog";
            str3 = str3 + " ([EntryDate], ErrorMessage, CodeClassMethodName)";
            str3 = str3 + " values(getdate()";
            str3 = str3 + ", '" + TransmittalKey + "| " + myErrorText + "'";
            str3 = str3 + ", 'DTFSvr.asp/addSelectedFile_Aspera')";

            cmdTemp3.CommandText = str3;
            cmdTemp3.CommandType = 1;
            cmdTemp3.Execute();
            cmdTemp3 = null;
            DataConn3.Close();
            DataConn3 = null;
        }
        DataConn.Close();

        DataConn = null;

        return (AddedFileKeyValue);
    }
    catch (err) {
        var cmdTemp2 = Server.CreateObject("ADODB.Command");
        var DataConn2 = Server.CreateObject("ADODB.Connection");
        DataConn2.ConnectionTimeout = Application("DataConn_ConnectionTimeout");
        DataConn2.CommandTimeout = Application("DataConn_CommandTimeout");
        DataConn2.Open(Application("DataConn_ConnectionString"), Application("DataConn_RuntimeUserName"), Application("DataConn_RuntimePassword"));
        cmdTemp2.ActiveConnection = DataConn2;
        var str2 = "";
        str2 = "insert into bms_ErrorLog";
        str2 = str2 + " ([EntryDate], ErrorMessage, CodeClassMethodName)";
        str2 = str2 + " values(getdate()";
        str2 = str2 + ", '" + TransmittalKey + "| " + err.number + "| " + err.description + "| " + err.message + "'";
        str2 = str2 + ", 'DTFSvr.asp/addSelectedFile_Aspera')";

        cmdTemp2.CommandText = str2;
        cmdTemp2.CommandType = 1;
        cmdTemp2.Execute();

        cmdTemp2 = null;
        DataConn2.Close();
        DataConn2 = null;
        return "";
    }
}
Pointy
  • 405,095
  • 59
  • 585
  • 614
Edward
  • 105
  • 1
  • 8
  • Okay - we need to clarify a couple of things: this is JScript not JavaScript; what database system are you using? – Paul Sep 24 '14 at 07:44
  • @Paul I always find that a bit confusing as in reality they are one and the same thing, I prefer to term it "server side" JavaScript. [What's the difference between JavaScript and JScript?](http://stackoverflow.com/a/135256/692942), answer apart from two exceptions not a lot. – user692942 Sep 24 '14 at 08:31
  • @Lankymart: There [is a difference](http://en.wikipedia.org/wiki/JScript), though, purely for the fact that it is server side and can use services and objects on the server. Saying that JavaScript and JScript are the same is like saying that Firefox and Chrome are the same - though they both display the same web pages there are big differences in how they work (I know - an extreme example there, but you know what I mean!). Confusing JScript with JavaScript and attempting to open databases on the client side could be ... interesting! – Paul Sep 24 '14 at 09:39
  • I am using SQL server 2008. My initial thought is to break the function up and try to isolate the error that way. One potential problem is that a database connection is open the entire time. This website is JavaScript. It is using ASP files and a technology called remote scripting to execute the ASP files that interact with the database. – Edward Sep 24 '14 at 13:31
  • Another idea is to use a stored procedure with an output parameter to return any error codes. – Edward Sep 24 '14 at 13:34
  • ` if (DataConn.Errors.Count > 0) { for (var i = 1;` should not that be beginning with zero ? – Flakes Sep 25 '14 at 14:11
  • Yes. I found that in another ADO reference. I copied the other code from a MSDN posting. – Edward Sep 25 '14 at 18:48

0 Answers0