Introduction

In this article, I will explain how to update a record in a database using a TextBox in JavaScript.
Note: This program will work only with Internet Explorer.
First I created a database EmpDetail. I created a table in this database.
Query Code
  1. CREATE TABLE [dbo].[EmpSalary_Info](
  2. [id] [int] NULL,
  3. [name] [varchar](50) NULL,
  4. [salary] [int] NULL
  5. ) ON [PRIMARY]
Now insert some data into the "EmpSalary_Info" table. Then use the following procedure.
Complete Program
Update_Record.html
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title></title>
  5. <script type="text/javascript">
  6. function ShowAll()
  7. {
  8. var connection = new ActiveXObject("ADODB.Connection");
  9. var connectionstring = "Data Source=.;Initial Catalog=EmpDetail;Persist Security Info=True;User ID=sa;Password=password@123;Provider=SQLOLEDB";
  10. connection.Open(connectionstring);
  11. var rs = new ActiveXObject("ADODB.Recordset");
  12. rs.Open("select * from EmpSalary_Info ", connection);
  13. rs.MoveFirst();
  14. var span = document.createElement("span");
  15. span.style.color = "Blue";
  16. span.innerText = " ID " + " Name " + " Salary";
  17. document.body.appendChild(span);
  18. while (!rs.eof) {
  19. var span = document.createElement("span");
  20. span.style.color = "green";
  21. span.innerText = "\n " + rs.fields(0) + " | " + rs.fields(1) + " | " + rs.fields(2);
  22. document.body.appendChild(span);
  23. rs.MoveNext();
  24. }
  25. rs.close();
  26. connection.close();
  27. }
  28. function UpdateRecord()
  29. {
  30. var txtid = document.getElementById('txtid').value;
  31. var txtname = document.getElementById('txtname').value;
  32. if (txtid.length != 0 && txtname.length!=0) {
  33. var connection = new ActiveXObject("ADODB.Connection");
  34. var connectionstring = "Data Source=.;Initial Catalog=EmpDetail;Persist Security Info=True;User ID=sa;Password=password@123;Provider=SQLOLEDB";
  35. connection.Open(connectionstring);
  36. var rs = new ActiveXObject("ADODB.Recordset");
  37. rs.Open("update EmpSalary_Info set EmpName = '" + txtname + "' where EmpId=" + txtid,connection);
  38. alert("Update Record Successfuly");
  39. txtid.value = " ";
  40. connection.close();
  41. }
  42. else
  43. {
  44. alert("Please textbox's value");
  45. }
  46. }
  47. </script>
  48. <style type="text/css">
  49. #txtname
  50. {
  51. z-index: 1;
  52. left: 230px;
  53. top: 94px;
  54. position: absolute;
  55. }
  56. #txtid
  57. {
  58. z-index: 1;
  59. left: 230px;
  60. top: 54px;
  61. position: absolute;
  62. }
  63. </style>
  64. </head>
  65. <body style="height: 89px">
  66. <div id="show"
  67. style="font-size: x-large; font-weight: bold; height: 185px; color: #009999;">
  68. Update Employee Record<p style="font-size: medium; color: #000000;">
  69. Enter Employee ID
  70. <input id="txtid" type="text" /></p>
  71. <p style="font-size: medium; color: #000000;">
  72. Update Employee Name
  73. </p>
  74. <input id="txtname" type="text" /><p>
  75. <input id="ShowRecord" type="button" value="Update" onclick="UpdateRecord()"
  76. style="font-weight: bold" />
  77. <input id="showall" type="button" value="Show All Record" onclick="ShowAll()"
  78. style="font-weight: bold" /></p>
  79. </div>
  80. </body>
  81. </html>
Output 1
First-image.jpg
Click on the "Show All Record" button.
click-on-show-button.jpg
Output 2
Enter an employee id in the TextBox that you want to update in the database and then enter the new employee name in the TextBox. Then click on the "Update" button.
click-on-update-button.jpg
Output 3
After updating the record, click on the "Show All Record" button. You will see a record updated successfully.
again-click-on-show-button.jpg
Output 4
If you will click on the Update button without entering a value into the TextBox then it will show the error:
error-msg.jpg
For more information, download the attached sample application.