I have a very simple Javascript function that hits a MS SQL server and returns some records back. There is a cell that i want to only display in the top table row when it is unique. I believe my problem is var hoisting because the variable that i assign within the while loop does not work because the value is not carried from the last records to compare. Here is the code
function searchIndex()
{
var termcounter = "";
flyoutHTML = '<table>';
var adOpenDynamic = 2
var adLockOptimistic = 3
var conn = new ActiveXObject("ADODB.Connection");
var connectionstring = "Provider=SQLOLEDB;Server=XXXXXXXX;INTEGRATED SECURITY=SSPI;DATABASE=YYYYYYYYYY;"
conn.Open(connectionstring)
var rs = new ActiveXObject("ADODB.Recordset")
rs.Open("SELECT field1, field2, field4, field4, field5 FROM dbo.table;", conn)
if (rs.eof)
{
flyoutHTML += '<tr><td align="center">No Records Found!</td></tr>';
}
else
{
while(!rs.eof)
{
if (termcounter != rs(0))
{
var termcounter = rs(0);
flyoutHTML += '<tr>';
flyoutHTML += '<td colspan="3">' + rs(0) + '</td>';
flyoutHTML += '</tr>';
}
flyoutHTML += '<tr>';
flyoutHTML += '<td>' + rs(1) + '</td><td>' + rs(2) + '</td><td>' + rs(3) + '</td>';
flyoutHTML += '</tr>';
rs.movenext
}
rs.close
conn.close
flyoutHTML += '</table>';
}