Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to insert ado recordset data with javascript

Status
Not open for further replies.

bslintx

Technical User
Apr 19, 2004
425
US
hey all...

i'm retrieving a recordset via javascript and ado. the ado method i am using is getstring. i am used to server side code...and i was wondering why i cannot get a string returned by getstring to work correctly in the following:

Code:
<script>
   var sDatabasePath = "tree.mdb";
   var cn = new ActiveXObject("ADODB.Connection");
   var strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sDatabasePath;
   
   cn.Open(strConn);
   var rs = new ActiveXObject("ADODB.Recordset");
   var SQL = "select Ticker, [Company Name], [Market Cap], Sales, Employees from tblMarket";
           
   rs.Open(SQL, cn,2,3);
   
   if(!rs.eof)
             {
              var sRecs = '["' + rs.GetString(2,-1,'","','"],["','&nbsp;');
              var s = String(sRecs).substring(0,String(sRecs).length-3)
             }


 	var myData = [
                              s
                             ];
                       
    
		var myColumns = [
			"Ticker", "Company Name", "Market Cap.", "$ Sales", "Employees"
		];
	</script>
</head>
<body>
	<script>

	//	create ActiveWidgets Grid javascript object
	var obj = new AW.UI.Grid;
	obj.setId("myGrid");

	//	define data formats
	var str = new AW.Formats.String;
	var num = new AW.Formats.Number;

	obj.setCellFormat([str, str, str, str, str]);

	//	provide cells and headers text
	obj.setCellText(s);
	obj.setHeaderText(myColumns);

	//	set number of rows/columns
	obj.setRowCount(20);
	obj.setColumnCount(5);

	//	enable row selectors
	obj.setSelectorVisible(true);
	obj.setSelectorText(function(i){return this.getRowPosition(i)+1});

	//	set headers width/height
	obj.setSelectorWidth(28);
	obj.setHeaderHeight(20);

	//	set row selection
	obj.setSelectionMode("single-row");

	//	set click action handler
	obj.onCellClicked = function(event, col, row){window.status = this.getCellText(col, row)};

	//	write grid html to the page
	document.write(obj);

	</script>

if i write out the value of var myData and 'hard code' into the code it works;

Code:
var myData = [
                              ["Wmt","Walmart","10","10","joe"]

                             ];

however...returning as a recordset...although it populates the grid...it populates the grid with ["Wmt","Walmart","10","10","joe"] in each cell

...thanks
 
[1] The array is not constructed that way. This instead.
[tt]
[red]/*[/red]
if(!rs.eof)
{
var sRecs = '["' + rs.GetString(2,-1,'","','"],["','&nbsp;');
var s = String(sRecs).substring(0,String(sRecs).length-3)
}
[red]*/[/red]
if(!rs.eof) {
sRec=rs.GetString(2,-1,",","\n","&nbsp;");
myData = sRecs.split(/\n/);
for (var i=0; i<aRec.length; i++) {
myData=myData.split(/,/);
}
}
[/tt]
[2] Your row and column setting should be made consistent. It is much more satisfactory to script like this.
[tt]
// set number of rows/columns
obj.setRowCount([blue]myData.length[/blue]);
obj.setColumnCount([blue]myColumns.length[/blue]);
[/tt]
[3] You should close the rs, in any case, after the conditional block of rs.eof. So add this.
[tt]
rs.close();
[/tt]
[4] With the above, the big plan should be more or less ready for the testing.
 
tsuji,

i truely appreciate the response...unfortunately...using the for loop pretty much defeats the purpose of me using getstring...i may as well use getrows

i therefore used fso to generate a comma delimited getstring and this satisfies the speed i need vs looping

yes...i close the recordset...i didn't have it in the posted code...but thanks for the insight...

again...thanks for your time!
 
What is wrong using a for loop? Besides, it is used against the getstring method output. And that I detect you don't understand how js array is made via getstring method. Sure you can use getrow. I am not going to argue one way or another. Now, you said you prefer using again another object fso that I know very well. This time, I might argue the idea... maybe not after all. It is up to you, and I have nothing to add.
 
again....speed

i understand the loop is used against the getstring ...i also understand as i mentioned i may as well use get rows...a 2d array....the loop is FAR slower in generating the recordset reslts....the code works as is if on server side...no loops...just getstring.....it's lightning quick vs a delay in looping...very noticable difference...but i'm using in a .hta...no server

you may feel it's debateable ...but the fact of the matter is in my case...i had to come up with the quickest way to post the recordset...and looping was definitely not the option...

using the fso was on a hunch/whim...i do not use js often...but i do know logic...using the comma generated file created by the fso works exactly the way i wanted....and it not only serves as a catalyst for the js...i found it to be beneficial in trend analysis...since it was stored as a file and could be looked up via script

again...i know i through a curveball out there when i mentioned fso...this was not intentional...but sometimes thinking outside of the box you may come up with perhaps a more unorthodox method...

as always...thanks for the help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top