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 RajTOP 100Posted Aug 28, 2016, 3:18 AM EDT
Nice
Bavani MPosted Jun 19, 2016, 8:09 PM EDT
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 29, 2016, 9:51 PM EST
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 18, 2015, 8:24 PM EDT
Thank you Nipesh Janghel
Nipesh JanghelPosted Aug 18, 2015, 5:59 AM EDT
nice!!
Rajeesh MenothPosted Aug 18, 2015, 4:08 AM EDT
Thank you Saritha K T
Saritha K TPosted Aug 18, 2015, 12:22 AM EDT
nice information
Rajeesh MenothPosted Aug 12, 2015, 8:35 PM EDT
Thank you Santhakumar Munuswamy sir
Santhakumar MunuswamyPosted Aug 12, 2015, 11:08 AM EDT
Good Article. Thanks for sharing
Rajeesh MenothPosted Aug 10, 2015, 10:40 PM EDT
Thank you Sibeesh Venu sir
Sibeesh VenuTOP 100Posted Aug 10, 2015, 10:23 PM EDT
Nice Share
Rajeesh MenothPosted Aug 10, 2015, 8:34 PM EDT
Thanks Shridhar Sharma
Shridhar SharmaPosted Aug 10, 2015, 1:26 PM EDT
good one :)
Rajeesh MenothPosted Aug 10, 2015, 9:19 AM EDT
Thank you Nipesh Janghel
Nipesh JanghelPosted Aug 10, 2015, 8:00 AM EDT
Nice!!
Rajeesh MenothPosted Aug 10, 2015, 6:59 AM EDT
Thank you Rakesh Chavda sir
RakeshPosted Aug 10, 2015, 6:54 AM EDT
Nice concept
Rajeesh MenothPosted Aug 10, 2015, 6:06 AM EDT
Thanks Munesh Sharma
Munesh SharmaPosted Aug 10, 2015, 6:04 AM EDT
Nice one
Rajeesh MenothPosted Aug 10, 2015, 5:35 AM EDT
Thanks Gopi Chand
Gopi ChandPosted Aug 10, 2015, 5:34 AM EDT
Nice work
Rajeesh MenothPosted Aug 10, 2015, 12:15 AM EDT
Thank you Rahul Prajapat
Rahul PrajapatPosted Aug 10, 2015, 12:12 AM EDT
Nice article sir
Rajeesh MenothPosted Aug 9, 2015, 11:53 PM EDT
Thank you Debasis Saha
Debasis SahaTOP 100Posted Aug 9, 2015, 11:34 PM EDT
Good one....
Rajeesh MenothPosted Aug 9, 2015, 10:17 PM EDT
Thank you Ankit Bansal
Ankit BansalPosted Aug 9, 2015, 10:06 PM EDT
thanks for sharing...really helpfull
Manas MohapatraPosted Aug 9, 2015, 9:02 PM EDT
Good one. Very helpful for beginners...
Upendra Pratap ShahiPosted Aug 9, 2015, 9:01 PM EDT
nice one sir
Sibeesh VenuTOP 100Posted Aug 9, 2015, 9:01 PM EDT
Nice Share
Kalyankumar RavichandranPosted Aug 9, 2015, 8:51 PM EDT
Good one Rajeesh!
Nilesh JadavPosted Aug 9, 2015, 8:11 PM EDT
Nice one Rajeesh Menoth sir
Chervine BhiwooPosted Aug 9, 2015, 7:36 PM EDT
Very nice! Thanks!
Mohammed IbrahimPosted Aug 9, 2015, 6:55 PM EDT
nice
Van LaiPosted Aug 9, 2015, 6:07 PM EDT
Nice one, thx for sharing.
Pankaj Kumar ChoudharyTOP 100Posted Aug 9, 2015, 3:57 PM EDT
Nice Explain and also useful article..........