Introduction

In this article, I explain how to show whether a TextBox value exists in the database using JavaScript.
Note: This program will work only with Internet Explorer.
First I created a database "EmpSalary_Info". 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_Data.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 ShowDB()
  7. {
  8. var txtid = document.getElementById('txtid').value;
  9. if (txtid.length != 0)
  10. {
  11. var connection = new ActiveXObject("ADODB.Connection");
  12. var connectionstring = "Data Source=.;Initial Catalog=EmpDetail;Persist Security Info=True;User ID=sa;Password=password@123;Provider=SQLOLEDB";
  13. connection.Open(connectionstring);
  14. var rs = new ActiveXObject("ADODB.Recordset");
  15. rs.Open("select * from EmpSalary_Info where EmpId=" + txtid, connection);
  16. rs.MoveFirst();
  17. var span = document.createElement("span");
  18. span.style.color = "Blue";
  19. span.innerText = " ID " + " Name " + " Salary";
  20. document.body.appendChild(span);
  21. while (!rs.eof)
  22. {
  23. var span = document.createElement("span");
  24. span.style.color = "Red";
  25. span.innerText = "\n " + rs.fields(0) + " | " + rs.fields(1) + " | " + rs.fields(2);
  26. document.body.appendChild(span);
  27. rs.MoveNext();
  28. alert("Employee exit in Database");
  29. }
  30. rs.close();
  31. connection.close();
  32. }
  33. else
  34. {
  35. alert("Please Enter Employee Id In TextBox");
  36. }
  37. }
  38. </script>
  39. <style type="text/css">
  40. #main
  41. {
  42. height: 264px;
  43. }
  44. </style>
  45. </head>
  46. <body>
  47. <div id="show" style="font-size: x-large; font-weight: bold; height: 142px; color: #993333;">
  48. Employee Details<p style="font-size: medium; color: #000000;">
  49. Enter Employee Id
  50. <input id="txtid" type="text" /></p>
  51. <p>
  52. <input id="ShowRecord" type="button" value="Show" onclick="ShowDB()" style="font-weight: bold" /> </p>
  53. </div>
  54. </body>
  55. </html>
Output 1
First-image.jpg
Enter an employee id and then click on the "Show" button.
second-image.jpg
Output 2
We are not entering a value into the TextBox and we click on the "Show" button.
error-msg.jpg
For more information, download the attached sample application.