connect to database using javascript
-
I have to Get data from database and fill data into ListBox using javascript. I did like below var strConn = 'Provider=SQLOLEDB;server=localhost;user id=sa;pwd=***;initial catalog=xxx;'; var cn = new ActiveXObject("ADODB.Connection"); cn.open(strConn); var rs = new ActiveXObject("ADODB.Recordset"); rs.Open('SELECT EmpId,Name FROM Employee', cn); rs.MoveFirst while(!rs.eof) { var opt = document.createElement("Option"); opt.text = rs.fields(1); opt.value = rs.fields(0); document.getElementById(ListBoxCtrlName).options.add(opt); rs.movenext; } rs.close; cn.close; This is working fine in IE only when access datasource across domains enabled otherwise listbox not getting data.How can I fix this issue in IE And FireFox
-
I have to Get data from database and fill data into ListBox using javascript. I did like below var strConn = 'Provider=SQLOLEDB;server=localhost;user id=sa;pwd=***;initial catalog=xxx;'; var cn = new ActiveXObject("ADODB.Connection"); cn.open(strConn); var rs = new ActiveXObject("ADODB.Recordset"); rs.Open('SELECT EmpId,Name FROM Employee', cn); rs.MoveFirst while(!rs.eof) { var opt = document.createElement("Option"); opt.text = rs.fields(1); opt.value = rs.fields(0); document.getElementById(ListBoxCtrlName).options.add(opt); rs.movenext; } rs.close; cn.close; This is working fine in IE only when access datasource across domains enabled otherwise listbox not getting data.How can I fix this issue in IE And FireFox
DONT DO THIS! I CANNOT STRESS STRONGLY ENOUGH HOW BAD THE CODE ABOVE IS. 1) Exposing a connection string in javascript code 2) Exposing the sa password in javascript code :~ 3) Relying on ActiveX, which is IE only (this one answers your question). 4) In any case, most uses of IE will get a security warning when running this code, it is inherantly unsafe to allow this code to run. As to how normal, qualified people "fill a listbox from a database using javascript" - they use AJAX, which is to say javascript makes an HTTP request to a serverside script (probably ASP.NET in your case) which returns the data only to javascript for filling the listbox. At no time can the user see anything about the database connection.
-
DONT DO THIS! I CANNOT STRESS STRONGLY ENOUGH HOW BAD THE CODE ABOVE IS. 1) Exposing a connection string in javascript code 2) Exposing the sa password in javascript code :~ 3) Relying on ActiveX, which is IE only (this one answers your question). 4) In any case, most uses of IE will get a security warning when running this code, it is inherantly unsafe to allow this code to run. As to how normal, qualified people "fill a listbox from a database using javascript" - they use AJAX, which is to say javascript makes an HTTP request to a serverside script (probably ASP.NET in your case) which returns the data only to javascript for filling the listbox. At no time can the user see anything about the database connection.
Connection string and Getting data from database in .js file. So its not visible.
-
Connection string and Getting data from database in .js file. So its not visible.
Js files can be downloadable by normal users even don't need hackers. It's totally a wrong way, use AJAX as J4amieC said.
thatraja
-
Connection string and Getting data from database in .js file. So its not visible.