[script language = "VBscript" runat = "Server"] function dbug(ss) Response.Clear() Response.Write(ss) Response.End() end function '--dbug function fnLOWER(ss) fnLLOWER = LCASE(ss) end function [/script [script language = jscript runat = server] /*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--* * FUNCTION: fnShowRecord(...) * DESCRIPTION: This function displays a single DATABASE record on an ASP page. * STATUS: working order with moderate testing * DEPENDENCIES: fnErrorMessage() * DOCUMENTED AT: http://www.geocities.com/matth3wbishop/eg/asp/ * CODE LOCATION: http://www.geocities.com/matth3wbishop/eg/asp/ * TO DO: a) combine the ooConnection and the aaConnect parameters b) allow the passing of a recordset (as well as open string) c) allow a better style, eg alternating colours, no border *--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*/ function fnShowRecord( strOpen, ooConnection, aaConnect, aaLabels, aaColourInfo) { var bCloseConnection = new Boolean(false); var ooRecord; //The Ado recordset variable var sConn; //Temp connection string var aaFieldLabels = new Array(); //Text labels for the field values var aaColours = new Array(); //Colours for cells and text var aaConn = new Array(); //Hold the data connection info //dbug(aaFieldLabels.getItem(0)); /*--*--*--*--*--*--*--*--*--*--*--*--*--*--*-- START ERROR HANDLING --*--*--*--*--*--*--*--*--*--*--*--*--*--*--*/ if (strOpen == null) { fnErrorMessage( "fnShowRecord", "strOpen", "The parameter was omitted. It is required"); } /* if */ if ((ooConnection == null) && (aaConnect == null)) { fnErrorMessage( "fnShowRecord", "ooConnection, aaConnect", "Either the 'ooConnection' or the 'strConnection' parameter<br>" + "is required."); } /* if */ if ((ooConnection == null) && (aaConnect != null)) { aaConn = fnConvertArray(aaConnect); //--var aaTemp = new VBArray(aaConnect); //--aaConn = aaTemp.toArray(); var sConn; //temp string variable //***Provide some default values if (aaConn[0] == null) {aaConn[0] = "sql server";} if (aaConn[1] == null) {aaConn[1] = "(local)";} if (aaConn[2] == null) {aaConn[2] = "sa";} if (aaConn[3] == null) {aaConn[3] = "";} sConn = "DRIVER={" + aaConn[0] + "};"; sConn += "SERVER=" + aaConn[1]; ooConnection = Server.CreateObject("ADODB.connection"); //dbug(sConn); ooConnection.Open(sConn, aaConn[2], aaConn[3]); if (aaConn[4] != null) {ooConnection.DefaultDatabase = aaConn[4];} bCloseConnection = true; } /* if no connection */ ooRecord = Server.CreateObject("ADODB.recordset"); ooRecord.ActiveConnection = ooConnection; //***different types of open strings var regQ = /([^ ,@;\/]+).*/gi; var regP = /\bselect\b/gi; var regR = /([^ ,@;\/]+)(.)(\d{1,2})/gi; var sQ; //***Holds the query string var sTemp; //***Temporary variable if (regP.test(strOpen) == false) { //***Handle the 'TABLENAME,DIGIT' format for //***specifying a record if (regR.test(strOpen) == true) { //INCOMPLETE } /* if */ //***handle raw table names sTemp = strOpen.replace(regQ, "$1"); sQ = "sp_tables '" + sTemp + "'"; //dbug(sQ); ooRecord.Open(sQ); if (!ooRecord.EOF) { strOpen = "SELECT * FROM " + sTemp; } /* if */ else { //***handle stored procedures ooRecord.Close(); var sQ = "sp_stored_procedures '" + sTemp + "'"; ooRecord.Open(sQ); if (!ooRecord.EOF) { //do nothing: the string is okay } /* if is a stored proc */ else { fnErrorMessage( "fnShowRecord", "strOpen ('" + strOpen + "')", "The parameter is neither a SQL SELECT statement,<br>" + "nor the name of a Table, nor the name of a stored procedure<br>" + "in the database. The parameter must be one of these"); } /* if, else */ } /* if, else */ ooRecord.Close(); } /* if no 'select' in the string */ ooRecord.Open(strOpen); if (!ooRecord.EOF) { //dbug(ooRecord("description")); /*--*--*--*--*--*--*--*--*--*--*--*--*--*--*-- PROVIDE DEFAULT VALUES --*--*--*--*--*--*--*--*--*--*--*--*--*--*--*/ if (aaLabels == null) { aaFieldLabels[0] = "Record"; for (var ii = 0; ii < ooRecord.Fields.Count; ii++) { aaFieldLabels[ii+1] = ooRecord.Fields.Item(ii).Name; } /* for */ } else { //COULD USE FNCONVERTARRAY HERE //--aaFieldLabels = fnConvertArray(aaLabels); aaFieldLabels = aaLabels; if (aaFieldLabels[0] == null) {aaFieldLabels[0] = "Record";} for (var i = 0; i < ooRecord.Fields.Count; i++) { if (aaFieldLabels[i+1] == null) { aaFieldLabels[i+1] = ooRecord.Fields.Item(i).Name; } } /* for */ } /* if */ if (aaColourInfo == null) { aaColours[0] = "white"; aaColours[1] = "white"; aaColours[2] = "white"; aaColours[3] = "black"; aaColours[4] = "black"; } else { aaColours = aaColourInfo; //-- aaColours = fnConvertArray(aaColourInfo); //-- var aaTemp = new VBArray(aaColourInfo); //-- aaColours = aaTemp.toArray(); if (aaColours[0] == null) //table colour { aaColours[0] = "white"; } if (aaColours[1] == null) //label cell colour { aaColours[1] = "white"; } if (aaColours[2] == null) //value cell colour { aaColours[2] = "white"; } if (aaColours[3] == null) //label font colour { aaColours[3] = "black"; } if (aaColours[4] == null) //value font colour { aaColours[4] = "black"; } } /* if */ sReturn = "<table name = tblDisplay \n" + " bgcolor = " + aaColours[0] + "\n" + " border = 1> \n"; var sFieldValue = new String(""); if (sReturn != "ERROR:") { sReturn += "<tr> \n" + " <td colspan = '2' \n" + " align = 'center' \n" + " bgcolor = 'black'> \n" + " <b><font size = +1 color = 'white'>" + Server.HtmlEncode(aaFieldLabels[0] + " ") + "\n" + " </font></b></td> \n" + "</tr> \n"; for (i = 0; i < ooRecord.Fields.Count; i++) { sFieldValue = new String(ooRecord.Fields(i).Value); sFieldValue = Server.HTMLEncode(sFieldValue + " "); sFieldValue = sFieldValue.replace(/\n/gi, "<br>\n"); sFieldValue = sFieldValue.replace(/\t/gi, " "); sFieldValue = sFieldValue.replace(/ /gi, " "); sReturn += "<tr> \n" + " <td bgcolor = " + aaColours[1] + "> \n" + " <font color = " + aaColours[3] + ">" + aaFieldLabels[i+1] + "</font> \n" + " </td> \n" + " <td bgcolor = " + aaColours[2] + "> \n" + " <font color = " + aaColours[4] + ">" + sFieldValue + " </font></td>\n" + "</tr>\n"; } /* for */ sReturn += "</table>"; } /* if no errors */ //dbug(Server.HTMLEncode(sReturn)); return sReturn; } else { return "The open string of the fnShowRecord function\n" + "did not return any records."; } /* if */ ooRecord.Close(); ooRecord = null; if (bCloseConnection == true) { ooConnection.Close(); ooConnection = null; } /* if */ aaLabels = null; } /* fnShowRecord */ [/script]