Introduction
This article explains how to automatically save a value into a SQL database using an ASP.Net WebMethod and jQuery Ajax.
jQuery
The following code shows that SaveDraft.aspx is the aspx page and AutoSave is the method. We pass the client-side value to the server-side using Ajax and WebMethod.
- $(document).ready(function () {
- // Configure to save every 5 seconds
- window.setInterval(saveDraft, 5000);//calling saveDraft function for every 5 seconds
- });
- // ajax method
- function saveDraft() {
- $.ajax({
- type: "POST",
- contentType: "application/json; charset=utf-8",
- url: "SaveDraft.aspx/AutoSave",
- data: "{'firstname':'" + document.getElementById('Firstname').value + "','middlename':'" + document.getElementById('Middlename').value + "','lastname':'" + document.getElementById('Lastname').value + "'}",
- success: function (response) {
- }
- });
- }
In the following code the saveDraft function fires every 5 seconds. The value will be saved in the database every 5 seconds without post-backs. Based on our requirements we can change the time interval.
- $(document).ready(function () {
- // Configure to save every 5 seconds
- window.setInterval(saveDraft, 5000);//calling saveDraft function for every 5 seconds
- });
Database structure
Create a table in a database for storing the personal information.The following SQL code contains an existing person's details. So we can change those values at runtime without a post-back and button-click.
- USE [TestDB]
- GO
- /****** Object: Table [dbo].[Autosave] Script Date: 08/07/2015 18:19:54 ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- CREATE TABLE [dbo].[Autosave](
- [Id] [int] NULL,
- [firstname] [nvarchar](50) NULL,
- [middlename] [nvarchar](50) NULL,
- [lastname] [nvarchar](50) NULL
- ) ON [PRIMARY]
- GO
- INSERT [dbo].[Autosave] ([Id], [firstname], [middlename], [lastname]) VALUES (1, N'Rajeesh', N'', N'Menoth')
C#
The WebMethod will catch the client-side value in the server side. In the given example I am passing the id directly into the update query. You can modify the code for your requirements.
- [WebMethod]
- public static string AutoSave(string firstname, string middlename, string lastname)
- {
- int id = 1;
- string ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["TestDb"].ConnectionString;
- SqlConnection con = new SqlConnection(ConnectionString);
- {
- string str = "Update Autosave set firstname='" + firstname + "',middlename= '" + middlename + "',lastname= '" + lastname + "' where Id=" + id + "";
- SqlCommand cmd = new SqlCommand(str, con);
- {
- con.Open();
- cmd.ExecuteNonQuery();
- return "True";
- }
- }
- }
Important Section
1. Namespace
The given namespace contains the WebMethod,database connection libraries.
- using System.Web.Services;
- using System.Data.SqlClient;
2. Reference
Add the following jQuery reference.
- <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
- script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>
Figure 1
Output
Figure 2
Reference
Summary
We learned how to automatically save a value into a SQL database using ASP.Net WebMethod and jQuery Ajax. I hope this article is useful for all .NET beginners.

Delpin Susai RajPosted Aug 28, 2016, 7:18 AM
Nice
Bavani MPosted Jun 20, 2016, 12:09 AM
nice article.... But if i use more than 100 controls in that page... how can i use that key & value in ajax.... instead of that.. any other way to save that value in DB. thanks in advance...
Ahmed AltimemyPosted Jan 30, 2016, 2:51 AM
Thank you very much .. It's very nice .. But can you help me to make this code inserting data instead of updating data please .. Thank you so much .
Rajeesh MenothPosted Aug 19, 2015, 12:24 AM
Thank you Nipesh Janghel
Nipesh JanghelPosted Aug 18, 2015, 9:59 AM
nice!!
Rajeesh MenothPosted Aug 18, 2015, 8:08 AM
Thank you Saritha K T
Saritha K TPosted Aug 18, 2015, 4:22 AM
nice information
Rajeesh MenothPosted Aug 13, 2015, 12:35 AM
Thank you Santhakumar Munuswamy sir
Santhakumar MunuswamyPosted Aug 12, 2015, 3:08 PM
Good Article. Thanks for sharing
Rajeesh MenothPosted Aug 11, 2015, 2:40 AM
Thank you Sibeesh Venu sir
Sibeesh VenuPosted Aug 11, 2015, 2:23 AM
Nice Share
Rajeesh MenothPosted Aug 11, 2015, 12:34 AM
Thanks Shridhar Sharma
Shridhar SharmaPosted Aug 10, 2015, 5:26 PM
good one :)
Rajeesh MenothPosted Aug 10, 2015, 1:19 PM
Thank you Nipesh Janghel
Nipesh JanghelPosted Aug 10, 2015, 12:00 PM
Nice!!
Rajeesh MenothPosted Aug 10, 2015, 10:59 AM
Thank you Rakesh Chavda sir
RakeshPosted Aug 10, 2015, 10:54 AM
Nice concept
Rajeesh MenothPosted Aug 10, 2015, 10:06 AM
Thanks Munesh Sharma
Munesh SharmaPosted Aug 10, 2015, 10:04 AM
Nice one
Rajeesh MenothPosted Aug 10, 2015, 9:35 AM
Thanks Gopi Chand
Gopi ChandPosted Aug 10, 2015, 9:34 AM
Nice work
Rajeesh MenothPosted Aug 10, 2015, 4:15 AM
Thank you Rahul Prajapat
Rahul PrajapatPosted Aug 10, 2015, 4:12 AM
Nice article sir
Rajeesh MenothPosted Aug 10, 2015, 3:53 AM
Thank you Debasis Saha
Debasis SahaPosted Aug 10, 2015, 3:34 AM
Good one....
Rajeesh MenothPosted Aug 10, 2015, 2:17 AM
Thank you Ankit Bansal
Ankit BansalPosted Aug 10, 2015, 2:06 AM
thanks for sharing...really helpfull
Manas MohapatraPosted Aug 10, 2015, 1:02 AM
Good one. Very helpful for beginners...
Upendra Pratap ShahiPosted Aug 10, 2015, 1:01 AM
nice one sir
Sibeesh VenuPosted Aug 10, 2015, 1:01 AM
Nice Share
Kalyankumar RavichandranPosted Aug 10, 2015, 12:51 AM
Good one Rajeesh!
Nilesh JadavPosted Aug 10, 2015, 12:11 AM
Nice one Rajeesh Menoth sir
Chervine BhiwooPosted Aug 9, 2015, 11:36 PM
Very nice! Thanks!
Mohammed IbrahimPosted Aug 9, 2015, 10:55 PM
nice
Van LaiPosted Aug 9, 2015, 10:07 PM
Nice one, thx for sharing.
Pankaj Kumar ChoudharyPosted Aug 9, 2015, 7:57 PM
Nice Explain and also useful article..........