In this article I provide a quick overview of how to insert a record into SQL Server using jQuery. You can do it in many ways such as using Generic Handlers and using a WCF service. Here you will see it using jQuery with Ajax in ASP.Net. First we create a database table named "TestTable".

Creating SQL Database Table

This database contains one table named test.

  1. CREATE TABLE [dbo].[TestTable]
  2. (
  3. [Name] [varchar](50) NULL,
  4. [Email] [varchar](100) NULL
  5. )

Now press F5 to execute the script above that looks like this:

TestTable-in-SQLServer.jpg

ASPX Page

Right-click your ASP.NET project then select "Add New Item" -> "New Page" and give it the name "Default.aspx" and add the following control into it:

Insert-Data-into-SQL-Server-using-JQuery-in-ASP.NET.jpg

Now click on the source tab and add the following code:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head id="Head1" runat="server">
  5. <title>AutoComplete Box with jQuery</title>
  6. <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
  7. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
  8. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>
  9. <script type="text/javascript">
  10. $(document).ready(function()
  11. {
  12. $('#Button1').click(function()
  13. {
  14. $.ajax({
  15. type: 'POST',
  16. contentType: "application/json; charset=utf-8",
  17. url: 'Default.aspx/InsertMethod',
  18. data: "{'Name':'" + document.getElementById('txtUserName').value + "', 'Email':'" + document.getElementById('txtEmail').value + "'}",
  19. async: false,
  20. success: function(response)
  21. {
  22. $('#txtUserName').val('');
  23. $('#txtEmail').val('');
  24. alert("Record Has been Saved in Database");
  25. },
  26. error: function()
  27. {
  28. console.log('there is some error');
  29. }
  30. });
  31. });
  32. });
  33. </script>
  34. </head>
  35. <body>
  36. <form id="form1" runat="server">
  37. <div class="demo">
  38. <div class="ui-widget">
  39. <label for="tbAuto">
  40. Enter UserName:
  41. </label>
  42. <asp:TextBox ID="txtUserName" runat="server" ClientIDMode="Static" Width="202px"></asp:TextBox>
  43. <br />
  44. <br /> Email:
  45. <asp:TextBox ID="txtEmail" runat="server" ClientIDMode="Static" Width="210px"></asp:TextBox>
  46. <br />
  47. <br />
  48. <asp:Button ID="Button1" runat="server" Text="Button" ClientIDMode="Static" />
  49. </div>
  50. </div>
  51. </form>
  52. </body>
  53. </html>

Now double-click on the default form and add the following .cs file code:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.SqlClient;
  4. using System.Web.Services;
  5. using System.Web;
  6. using System.Data;
  7. public partial class _Default: System.Web.UI.Page
  8. {
  9. protected void Page_Load(object sender, EventArgs e) {}
  10. [WebMethod]
  11. public static string InsertMethod(string Name, string Email)
  12. {
  13. SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=TestDB;User ID=sa;Password=Micr0s0ft");
  14. {
  15. SqlCommand cmd = new SqlCommand("Insert into TestTable values('" + Name + "', '" + Email + "')", con);
  16. {
  17. con.Open();
  18. cmd.ExecuteNonQuery();
  19. return "True";
  20. }
  21. }
  22. }
  23. }

Now run the application.

Insert-Data-into-SQL-Server-using-JQuery-in-ASP.NET-1.jpg

Now enter the name and email into the corresponding TextBox.

Insert-Data-into-SQL-Server-using-JQuery-in-ASP.NET-2.jpg

Now click on the Button control.

Insert-Data-into-SQL-Server-using-JQuery-in-ASP.NET-3.jpg

Now open the SQL Server database and check it.

Insert-Data-into-SQL-Server-using-JQuery-in-ASP.NET-4.jpg