Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. connect to database using javascript

connect to database using javascript

Scheduled Pinned Locked Moved JavaScript
helpjavascriptdatabasesysadminquestion
5 Posts 4 Posters 45 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SatyaKeerthi15
    wrote on last edited by
    #1

    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

    J 1 Reply Last reply
    0
    • S SatyaKeerthi15

      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

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      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.

      S 1 Reply Last reply
      0
      • J J4amieC

        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.

        S Offline
        S Offline
        SatyaKeerthi15
        wrote on last edited by
        #3

        Connection string and Getting data from database in .js file. So its not visible.

        T Y 2 Replies Last reply
        0
        • S SatyaKeerthi15

          Connection string and Getting data from database in .js file. So its not visible.

          T Offline
          T Offline
          thatraja
          wrote on last edited by
          #4

          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


          Tips/Tricks|Brainbench certifications|Facebook|Twitter

          1 Reply Last reply
          0
          • S SatyaKeerthi15

            Connection string and Getting data from database in .js file. So its not visible.

            Y Offline
            Y Offline
            Yusuf
            wrote on last edited by
            #5

            SatyaKeerthi15 wrote:

            So its not visible.

            says who?

            Yusuf May I help you?

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups