Introduction

In this article, I will explain how to delete a record from a database using a TextBox in TypeScript.
Note: This program will work only on Internet Explorer.
First I created a database "EmpDetail". Then 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
Show_Record_With_TextBox_Value.htm
  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. {
  20. var span = document.createElement("span");
  21. span.style.color = "green";
  22. span.innerText = "\n " + rs.fields(0) + " | " + rs.fields(1) + " | " + rs.fields(2);
  23. document.body.appendChild(span);
  24. rs.MoveNext();
  25. }
  26. rs.close();
  27. connection.close();
  28. }
  29. function DeleteRecord()
  30. {
  31. var txtid = document.getElementById('txtid').value;
  32. if (txtid.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("delete from EmpSalary_Info where EmpId=" + txtid, connection);
  38. alert("Delete Record Successfuly");
  39. txtid.value = " ";
  40. connection.close();
  41. }
  42. else
  43. {
  44. alert("Please Enter Employee Id in TextBox");
  45. }
  46. }
  47. </script>
  48. <style type="text/css">
  49. #main
  50. {
  51. height: 264px;
  52. }
  53. #ShowRecord
  54. {
  55. width: 67px;
  56. z-index: 1;
  57. left: 53px;
  58. top: 119px;
  59. position: absolute;
  60. }
  61. #showall
  62. {
  63. z-index: 1;
  64. left: 146px;
  65. top: 118px;
  66. position: absolute;
  67. }
  68. </style>
  69. </head>
  70. <body style="height: 186px">
  71. <div id="show"
  72. style="font-size: x-large; font-weight: bold; height: 185px; color: #009999;">
  73. Delete Employee Record<p style="font-size: medium; color: #000000;">
  74. Enter Employee Id
  75. <input id="txtid" type="text" /></p>
  76. <p>
  77. <input id="ShowRecord" type="button" value="Delete" onclick="DeleteRecord()" /> <input id="showall" type="button" value="Show All Record" onclick="ShowAll()" /></p>
  78. </div>
  79. </body>
  80. </html>
Output 1
first-image.jpg
Click on the "Show All Record" button.
showAllrecord.jpg
Output 2
Enter the employee id into the TextBox that you want to delete from the database. Then click on the "Delete" button.
delete-record.jpg
Output 3
After deleting the record, click on the "Show All Record" button. You will see that the record was deleted successfully.
after-delete-record.jpg
Output 4
If you will click on the Delete button without entering any value into the TextBox then it will show the error.
error-msg.jpg
For more information, download the attached sample application.