Reader Level:

Insert Data Into SQL Server Using jQuery in ASP.Net

Posted by Rohatash Kumar Articles | JQuery June 17, 2013
In this article I provide a quick overview of how to insert data into SQL Server using jQuery.
  • 0
  • 0
  • 47448

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.

CREATE TABLE [dbo].[TestTable](

      [Name] [varchar](50) NULL,

      [Email] [varchar](100) NULL


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


.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:


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

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">

<head id="Head1" runat="server">

    <title>AutoComplete Box with jQuery</title>

    <link href=""

        rel="stylesheet" type="text/css" />

    <script type="text/javascript" src=""></script>

    <script type="text/javascript" src=""></script>

       <script type="text/javascript">

        $(document).ready(function () {

            $('#Button1').click(function () {


                    type: 'POST',

                    contentType: "application/json; charset=utf-8",

                    url: 'Default.aspx/InsertMethod',

                    data: "{'Name':'" + document.getElementById('txtUserName').value + "', 'Email':'" + document.getElementById('txtEmail').value + "'}",

                    async: false,

                    success: function (response) {                       



                        alert("Record Has been Saved in Database");


                    error: function ()

                    { console.log('there is some error'); }







    <form id="form1" runat="server">

    <div class="demo">

        <div class="ui-widget">

            <label for="tbAuto">

                Enter UserName:


&nbsp;<asp:TextBox ID="txtUserName" runat="server" ClientIDMode="Static" Width="202px"></asp:TextBox>

            <br />

            <br />

            Email: <asp:TextBox ID="txtEmail" runat="server" ClientIDMode="Static" Width="210px"></asp:TextBox>

            <br />

            <br />           

            <asp:Button ID="Button1" runat="server" Text="Button" ClientIDMode="Static" />







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

using System;

using System.Collections.Generic;

using System.Data.SqlClient;

using System.Web.Services;

using System.Web;

using System.Data;


public partial class _Default : System.Web.UI.Page


    protected void Page_Load(object sender, EventArgs e)





    public static string InsertMethod(string Name, string Email) 


        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=TestDB;User ID=sa;Password=Micr0s0ft");


            SqlCommand cmd = new SqlCommand("Insert into TestTable values('" + Name + "', '" + Email + "')", con);




                return "True";






Now run the application.


Now enter the name and email into the corresponding TextBox.


Now click on the Button control.


Now open the SQL Server database and check it.