How to insert and access SQLServer DB using HTML,Javascript

Sep 11 2016 1:55 PM

Hi,

I am trying to insert data into SQL Server by using HTML5, CSS3 and JAVASCRIPT. After press a button all the values should store in the database and again access data from SQL Server and display in HTML5 table. But I can't understand how to do it. I am using Visual Studio 2010 for creating HTML pages and SQL Server Database.

Can any help me for finding correct solution for this?

I am unable to find working solution for this in Google search. Actually I found one solution for inserting and accessing data from SQL server. But it is not working.

I tried bellow code:

Source Link:

http://www.c-sharpcorner.com/uploadfile/5089e0/insert-record-in-database-using-textboxes-in-javascript/

Code: I followed all the steps as same in above link.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

<script type="text/javascript" >

function InsertRecord()

{

var txtid = document.getElementById('txtid').value;

var txtname = document.getElementById('txtname').value;

var txtsalary = document.getElementById('txtsalary').value;

var txtcity = document.getElementById('txtcity').value;

if (txtid.length != 0 || txtname.length !=0 || txtsalary.length !=0|| txtcity.length !=0)

{

var connection = new ActiveXObject("ADODB.Connection");

var connectionstring = "Data Source=.;Initial Catalog=EmpDetail;Persist Security Info=True;User ID=sa;Password=****;Provider=SQLOLEDB";

connection.Open(connectionstring);

var rs = new ActiveXObject("ADODB.Recordset");

rs.Open("insert into Emp_Info values('" + txtid + "','" + txtname + "','" + txtsalary + "','" + txtcity + "')", connection);

alert("Insert Record Successfuly");

txtid.value = " ";

connection.close();

}

else

{

alert("Please Enter Employee \n Id \n Name \n Salary \n City ");

}

}

function ShowAll()

{

var connection = new ActiveXObject("ADODB.Connection");

var connectionstring = "Data Source=.;Initial Catalog=EmpDetail;Persist Security Info=True;User ID=sa;Password=****;Provider=SQLOLEDB";

connection.Open(connectionstring);

var rs = new ActiveXObject("ADODB.Recordset");

rs.Open("select * from Emp_Info ", connection);

rs.MoveFirst();

var span = document.createElement("span");

span.style.color = "Blue";

span.innerText = " ID " + " Name " + " Salary" + " City ";

document.body.appendChild(span);

while (!rs.eof)

{

var span = document.createElement("span");

span.style.color = "green";

span.innerText = "\n " + rs.fields(0) + " | " + rs.fields(1) + " | " + rs.fields(2) + " | " + rs.fields(3);

document.body.appendChild(span);

rs.MoveNext();

}

rs.close();

connection.close();

}

</script>

<style type="text/css">

#main

{

height: 264px;

}

#ShowRecord

{

width: 67px;

z-index: 1;

left: 20px;

top: 257px;

position: absolute;

}

#showall

{

z-index: 1;

left: 114px;

top: 257px;

position: absolute;

}

</style>

</head>

<body style="height: 431px">

<div id="show"

style="font-size: x-large; font-weight: bold; height: 298px; color: #009999;">

Insert Employee Record<p style="font-size: medium; color: #000000;">

Employee Id&nbsp;&nbsp;

<input id="txtid" type="text" /></p>

<p style="font-size: medium; color: #000000;">

Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<input id="txtname" type="text" /></p>

<p style="font-size: medium; color: #000000;">

Salary&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<input id="txtsalary" type="text" /></p>

<p style="font-size: medium; color: #000000;">

City&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<input id="txtcity" type="text" /></p>

<input id="ShowRecord" type="button" value="Insert" onclick="InsertRecord()" />&nbsp;

<input id="showall" type="button" value="Show All Record" onclick="ShowAll()" /></div>

</body>

</html>

When I try to insert or access data by using above code it displays following error.

Error Description:

JavaScript runtime error: Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done”.

I tried to found solution for above error in that process I got below information about activeX object method.

Link for Refer:

https://support.microsoft.com/en-in/kb/269495


Answers (3)