Insert Data Using jQuery Ajax in Asp.net C# [Database MS SQLServer]

Insert data with jquery ajax: Here in this article will see how to insert data using jquery ajax asp.net C#. i.e. A simple example for adding records into the database ( MS SQL SERVER) using jquery ajax in Asp.net C#. In my previous post have also explained how to fetch record from the database using jquery ajax in asp.net as JSON result.i.e (asp.net web method return JSON result). Now in this article, we mainly focus on how to insert or add a record into the database without page postback, and this can be done with jquery AJAX method. We make an ajax call from client side which sends parameters (data) to the server-side, and on server-side we save these values into our database.

On server-side, we can handle this in many different ways such as by using Generic Handlers(ashx file), by using WebMethod (ASMX file), by using WCF services or with Asp.net Web API. Here will see a simple example to insert data into the database using jquery ajax call web method with parameters.

Example:

Step to insert data using jquery ajax in Asp.net C#.

Download and import jQuery library.

Add HTML Markup.

Create Class object.

Code behind: Create Webmethod (to insert records).

jQuery: Calling jquery Ajax method.

Am assuming you are aware of WebMethod (ASMX file). So let’s make this post simple here on the server-side (C#) will insert record using WebMethod.

# Download and import jQuery library.

As we are using jquery ajax method so before writing any code, first we will download and include latest jQuery file into our Webform (default.aspx page).You can host the jQuery file on the server, or you can direct use the Google-hosted jQuery library.

Here am adding some textboxes and a button tag in the webform ( default.aspx ). And on button click, will make jquery ajax method. In short will pass multiple parameters in ajax call jquery to save these parameters with server-side code. So this is how our HTML looks like as written below.

As by looking at the HTML structure, you already got the idea, what we are going to save. Yes, we are going to save UserDetails i.e. Firstname, LastName, and its Location into our database MS SQL Server.

# Create Class Object:

As we need topass JSON object from Client side to Server side, so we have to encode our JavaScript Object to JSON. And Decoding can be done on Server side. So that Javascript object can be converted into a C# class object. Let’s create a C# Class. Here in our case, we are going to save user details, so we created a new class named as userDetails having properties as firstname, lastname, location and also remember similar structure will be used for Javascript Object on client-side

# Code-behind: Create a Webmethod (which use to save records)

So far we are ready with HTML structure and with class object userDetails, so now let write a webmethod which will save the data into the database and we call this webmethod (Webservice) from client-side via jquery ajax. This is how our Webmethod code looks like as written below:

Note: To avoid SQL Injections, I always use parameterized query or using Stored procedure at my work. Check what is sql injection and how to prevent it? Best practice would be to use parameterized query to perform CRUD operations instead simple SQL statement

# Calling jquery Ajax method, and post / insert data on the server.

Please have a close look at this part as it’s an essential part of this article because jquery ajax method is mostly used in all the WebApplication. It’s good to have a nice understanding of it, so you can play with it while working on your project.

Nowon button click we call webmethod from jquery with parameters, and these parameters (data) save into the database. You may notice we send parameters as JSON object, as we already created the similar class object on the server-side.

This is how our jquery ajax method looks like as written below, which calls the webmethod addNewUser which we created earlier on the server-side.

Error 1: Occurs when the JSON object that we are passing to the server doesn’t have the same parameter name as per the C# web method. i.e our C# class object name should be same as our Javascript object.

Error 2: This error occurs most probably if you have set wrong webmethod name or wrong webmethod URL or else if the webmethod was written on Webpage i.e., (.ASPX page) then you need to declare it as Static.

Output:

Conclusion: By the end of this article you were able to call webmethod from jquery ajax. This article explains in Asp.net C# with the combination of jquery ajax method and Webservice (ASMX) webmethod How to insert or save data using jquery in asp.net c#. That is without page postback we can able to save data into the database MS SQL in our Asp.net C# Web Application.