We know that jQuery is a very important part of a JavaScript. jQuery is an open-source library for client-side programming or scripting.

Now I will explain here step-by-step how to insert text data into a database.

Step 1

Go to the database side.

  1. Create a PersonData table in the SQL database as in the following:
    1. create table PersonData
    2. (
    3. Name varchar(20),
    4. LName varchar(20)
    5. )
  2. Create a procedure to insert a data record into a table as in the following:
    1. create procedure sp_PersonData
    2. @Name varchar(20),
    3. @LName varchar(20)
    4. as
    5. begin
    6. set nocount off;
    7. insert into PersonData (Name,LName)values(@Name,@LName)
    8. End

Step 2

Go to the Visual Studio project side.

I. Go to the project in the Solution Explorer.

Select the project.
Right-click on it and select Add New Item.
Select the page as in the following figure.

Add Design Page
Figure 1: Add Design Page

II. Now go to the page design side.

Add a TextBox control and Button control as in the following:

Design Page
Figure 2: Design Page

III. Finally write scripting on the design page for the TextBox data insert as in the following:

  1. <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true" CodeBehind="DataInsertUseJQuery.aspx.cs" Inherits="Test_WebApplication.UI.DataInsertUseJQuery" %>
  2. <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
  3. </asp:Content>
  4. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
  5. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  6. <script type="text/javascript">
  7. function SavePersonRecord() {
  8. var Name = $.trim($('#<%=txtName.ClientID %>').val());
  9. var LName = $.trim($('#<%=txtLastName.ClientID %>').val());
  10. var Messege = "";
  11. if (Name == '') {
  12. Messege = "Can not Blank Name";
  13. }
  14. if (LName == '') {
  15. Messege += "Can not Blank Last Name";
  16. }
  17. if (Messege.length == 0) {
  18. $.ajax({
  19. type: "POST",
  20. dataType: "json",
  21. contentType: "application/json; charset=utf-8",
  22. url: "DataInsertUseJQuery.aspx/InsertPersonRecord",
  23. data: "{'Name':'" + Name + "', 'LName':'" + LName + "'}",
  24. success: function (Record) {
  25. $('#txtName').val();
  26. $('#txtLastName').val();
  27. if (Record.d == true) {
  28. $('#Result').text("Your Record insert");
  29. }
  30. else {
  31. $('#Result').text("Your Record Not Insert");
  32. }
  33. },
  34. Error: function (textMsg) {
  35. $('#Result').text("Error: " + Error);
  36. }
  37. });
  38. }
  39. else {
  40. $('#Result').html('');
  41. $('#Result').html(Messege);
  42. }
  43. $('#Result').fadeIn();
  44. }
  45. </script>
  46. <fieldset style="width: 250px"><legend> Data insert Use jQuery </legend>
  47. <h3 id="Result"></h3>
  48. <div>
  49. <asp:Table runat="server" >
  50. <asp:TableRow>
  51. <asp:TableCell>Name </asp:TableCell><asp:TableCell><asp:TextBox ID="txtName" runat="server" ></asp:TextBox></asp:TableCell>
  52. </asp:TableRow>
  53. <asp:TableRow>
  54. <asp:TableCell>Last Name</asp:TableCell><asp:TableCell><asp:TextBox ID="txtLastName" runat="server" ></asp:TextBox></asp:TableCell>
  55. </asp:TableRow>
  56. <asp:TableRow>
  57. <asp:TableCell></asp:TableCell><asp:TableCell><asp:Button ID="btnInsertRecord" runat="server" Text="Save" OnClientClick="SavePersonRecord(); return false"/></asp:TableCell>
  58. </asp:TableRow>
  59. </asp:Table>
  60. </div>
  61. </fieldset>
  62. </asp:Content>
IV. Now go to the page code side.

Here we will add:
  1. using System.Configuration;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. using System.Web.Services;
For the database connection string use the web method and a add connection string to the project's web.config file as in the following:
  1. <connectionStrings>
  2. <add name="connstr" connectionString="Data Source=RAKESH-PC;Initial Catalog=SqlServerTech;User ID=sa;Password=password" providerName="System.Data.SqlClient"/>
  3. <add name="Pratical_testConnectionString" connectionString="Data Source=RAKESH-PC;Initial Catalog=Pratical_test;User ID=sa" providerName="System.Data.SqlClient"/>
  4. </connectionStrings>
And finally insert code into the .aspx.cs side as in the following:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Configuration;
  8. using System.Data;
  9. using System.Data.SqlClient;
  10. using System.Web.Services;
  11. namespace Test_WebApplication.UI
  12. {
  13. public partial class DataInsertUseJQuery : System.Web.UI.Page
  14. {
  15. string conString = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
  16. protected void Page_Load(object sender, EventArgs e)
  17. {
  18. }
  19. [WebMethod]
  20. public static bool InsertPersonRecord(string Name, string LName)
  21. {
  22. bool InsertData;
  23. using (SqlConnection con = new SqlConnection(conString)
  24. {
  25. using (SqlCommand cmd = new SqlCommand("sp_PersonData", con))
  26. {
  27. cmd.CommandType = CommandType.StoredProcedure;
  28. cmd.Parameters.AddWithValue("@Name", Name);
  29. cmd.Parameters.AddWithValue("@LName", LName);
  30. if (con.State == ConnectionState.Closed)
  31. {
  32. con.Open();
  33. }
  34. int Result = cmd.ExecuteNonQuery();
  35. if (Result > 0)
  36. {
  37. InsertData = true;
  38. }
  39. else
  40. {
  41. InsertData = false;
  42. }
  43. return InsertData;
  44. }
  45. }
  46. }
  47. }
  48. }
V. Now you will run the page on the browser side by pressing the F5 button. You will finally see your design page as in the following:

Browser side Design Page
Figure 3: Browser side Design Page

Now fill in the record in the TextBox and click the Save button to save the record to the database.

Save Data
Figure 4: Save Data

Check in your database for the last inserted record as in the following:

check Record in Database
Figure 5: Check Record in Database

Finally now you understand how to insert a record with jQuery ajax.